PHP SQL + HTML checkboxes

Associate
Joined
1 Mar 2006
Posts
425
Hi


im just looking for a bit of help on the best way to do this..

so i have a sample SQL Database
2rhy9ok.jpg


now from that DB i want my HTML page to show checkboxes ticked if there is a "1" in the corresponding checkbox field


20u4cq8.jpg


can anyone advise on the best way to do this?

thanks ;)
 
Associate
OP
Joined
1 Mar 2006
Posts
425
hi

thanks for the response

im guessing i will have make the query a variable first?

eg:
Code:
$var = mysql_query("SELECT * FROM chkbx1")
 
Associate
OP
Joined
1 Mar 2006
Posts
425
how about

Code:
<?php
include_once 'includes/db.php';

$sql = "SELECT somecolumm FROM sometable";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$checked = $result['somecolumm'];

?>

<input type="checkbox" name="somecolumm" value="1" <?php if ($checked == 1) echo 'checked'; ?> />
 
Associate
Joined
24 Oct 2008
Posts
424
Location
W-s-M
I've expanded on denby's example as his is spot on, the only difference is that instead of "include_once" I would always use "require_once" whenever the database connection file is concerned, I'd rather have the script stop immediately if there's an error. ;)

Code:
<?php
	require_once "db.php";
	$sql 		= "SELECT * FROM database_name";
	$result 	= mysql_query($sql);
	$row 		= mysql_fetch_array($result);
	$chkbox1 	= $result['chkbx1'];
	$chkbox2 	= $result['chkbx2'];
?>
<table>
	<tr>
		<td>1</td>
		<td><input type="checkbox"
			<?php
				if ($chkbox1 == 1)
					echo " checked "
			?>	
		/></td>
	</tr>
	<tr>
		<td>2</td>
		<td><input type="checkbox"
			<?php
				if ($chkbox2 == 1)
					echo " checked "
			?>	
		/></td>
	</tr>
</table>
 
Associate
OP
Joined
1 Mar 2006
Posts
425
I've expanded on denby's example as his is spot on, the only difference is that instead of "include_once" I would always use "require_once" whenever the database connection file is concerned, I'd rather have the script stop immediately if there's an error. ;)

Code:
<?php
	require_once "db.php";
	$sql 		= "SELECT * FROM database_name";
	$result 	= mysql_query($sql);
	$row 		= mysql_fetch_array($result);
	$chkbox1 	= $result['chkbx1'];
	$chkbox2 	= $result['chkbx2'];
?>
<table>
	<tr>
		<td>1</td>
		<td><input type="checkbox"
			<?php
				if ($chkbox1 == 1)
					echo " checked "
			?>	
		/></td>
	</tr>
	<tr>
		<td>2</td>
		<td><input type="checkbox"
			<?php
				if ($chkbox2 == 1)
					echo " checked "
			?>	
		/></td>
	</tr>
</table>


Nice one , one thing though it doesn't say which table in the DB to pull data from

Should it be select * from database where. Colummname = chkbox1;

?
 
Associate
Joined
28 Jun 2009
Posts
466
Nice one , one thing though it doesn't say which table in the DB to pull data from

Should it be select * from database where. Colummname = chkbox1;

?

SELECT * FROM tableName


The SQL connection connects to the database, queries are then performed on the tables in that Database.
 
Associate
OP
Joined
1 Mar 2006
Posts
425
Hi

thanks for all your replies so far , it is greatly appreciated

now they are both constantly checked,even though one of the columns has a "0" in it


e9vfus.jpg






Code:
<?php

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'XXXXXXXXXX');
define('DB_PASSWORD', 'XXX');
define('DB_DATABASE', 'sbsuppli_game');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());

$sql = "SELECT * FROM game";
	$result 	= mysql_query($sql);
	$row 		= mysql_fetch_array($result);
	$chkbox1 	= $result['chkbx1'];
	$chkbox2 	= $result['chkbx2'];
	
?>
<table>
	<tr>
		<td>1</td>
		<td><input type="checkbox"
			<?php
				if ($chkbox1 == 1)
					echo " checked "
			?>	
		</td>
	</tr>
	<tr>
		<td>2</td>
		<td><input type="checkbox"
			<?php
				if ($chkbox2 == 1)
					echo " checked "
			?>	
		</td>
	</tr>
</table>
?>
 
Last edited:
Associate
Joined
28 Jun 2009
Posts
466
Ok. I don't have WAMP installed at the moment so can't quickly change it all.

It looks like you are crossing between php and html without stating that you are. Is this a .html file or a .php file?

If it is .html I think the check section should be like this:

<?php
if ($chkbox2 == 1)
{
echo ' checked="checked" ';
}
?>

The saving of the Database values to variables $chkbox1 and 2 are wrong. It should be:

$sql = "SELECT * FROM game";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$chkbox1 = $row['chkbx1'];
$chkbox2 = $row['chkbx2'];

I'll sort WAMP out in a minute and write it all out properly. If you want it as a php file all html content (The stuff the user actually sees) should be echoed.

e.g. echo ' <table></table>';
 
Associate
Joined
28 Jun 2009
Posts
466
Right. This is for a .php file to be run off the server, so game.php for instance. This code works, obviously change your database settings, sorry I changed it so much I'm not used the the DEFINE thing. I tend to put the db connection in an includes file and just require it at start of php code.

<?php

$host = "localhost";

$username = "root";

$password = "";

$database = "test";

$conn = mysql_connect($host, $username, $password) or die ("Could not connect");
$db = mysql_select_db($database, $conn) or die ("Could not select DB");

$strSQL = "SELECT * FROM game";
$result = mysql_query($strSQL);
$row = mysql_fetch_assoc($result);
$chkbox1 = $row['chkbx1'];
$chkbox2 = $row['chkbx2'];

echo '
<table>
<tr>
<td>1</td>
<td><input type="checkbox"';

if ($chkbox1 == 1)
{
echo ' checked="checked" ';
}
echo '
</td>
</tr>
<tr>
<td>2</td>
<td><input type="checkbox"
';
if ($chkbox2 == 1)
{
echo ' checked="checked" ';
}

echo '
</td>
</tr>
</table>
';
?>

http://pastebin.com/0bgxEGrN (For some coloured formatting)

Any problems just ask.
 
Associate
OP
Joined
1 Mar 2006
Posts
425
that works perfectly..thankyou

i shall go through it line by line and learn it for next time

thanks again
 
Associate
Joined
24 Oct 2008
Posts
424
Location
W-s-M
I think it should be checked="checked" not just checked.

Edit: Not sure if you need ; with inline php in a html doc. I usually just write pure php.
It was working for me with just checked but it works either way anyway, also if you don't use the curly brackets then you don't need a semi-colon but I suppose it is good practice to always use curly brackets and the semi-colon.
 
Top Bottom