Jump to content

Checkboxes And Mysql


deagle25

Recommended Posts

Hello!

I am a newbie with regards to php and I have a problem with showing and updating the value of a boolean (tinyint) field from a MySQL table on a webpage with a html table with all records and some php code that retrieves the data and shows it and lets the user update the data. It is working fine if I change the checkbox to a text field and updates e.g. 0 to 1 that way but I am not sure how to make it even show the boolean value in a checkbox. I suspect this is the faulty line "<td align="center"><input name="coded[]" type="checkbox" id="Coded" value="<?php echo $rows['Coded']; ?>"></td>".

Any help is appreciated!

 

This is all the code except the SQL connection lines:

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);

 

// Count table rows

$count=mysql_num_rows($result);

?>

 

<form name="form1" method="post" action="">

<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>

<td>

<table width="500" border="0" cellspacing="1" cellpadding="0">

 

 

<tr>

<td align="center"><strong>ID</strong></td>

<td align="center"><strong>Subject</strong></td>

<td align="center"><strong>Coded</strong></td>

</tr>

 

<?php

while($rows=mysql_fetch_array($result)){

?>

<tr>

<td align="center"><?php $id[]=$rows['ID']; ?><?php echo $rows['ID']; ?></td>

<td align="center"><input name="subject[]" type="text" id="Subject" value="<?php echo $rows['Subject']; ?>"></td>

<td align="center"><input name="coded[]" type="checkbox" id="Coded" value="<?php echo $rows['Coded']; ?>"></td>

</tr>

<?php

}

?>

<tr>

<td colspan="3" align="center"><input type="submit" name="submit" value="Submit"></td>

</tr>

</table>

</td>

</table>

</form>

 

<?php

// Check if button name "submit" is active, do this

if(isset($_POST['submit'])){

$subject = $_POST['subject'];

$coded = $_POST['coded'];

for($i=0;$i<$count;$i++){

$sql1="UPDATE $tbl_name SET Subject='$subject[$i]', Coded='$coded[$i]' WHERE ID='$id[$i]'";

$result1=mysql_query($sql1);

}

}

if(isset($result1)){header("location:coder4.php");

}

?>

Link to comment
https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/
Share on other sites

You need to set the text-box's "checked" attribute to show it as checked

EG

<input type='checkbox' name='coded[]' checked='checked' value=1 />

Thank you for the answer. Maybe I misunderstand your answer but I don't just want the checkboxes to be checked. I want them to reflect the value in the sql table behind.

 

Best regards

Ole

If you assign your value then you may be assigning a value of "0". If the user then checks the box then the value of "0" will be posted, not "1"

 

Use your value to see if it should be checked or not

 

<?php 
$check = ($rows['Coded'] == 1) ? 'checked="checked"' : '';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.