deagle25 Posted November 19, 2012 Share Posted November 19, 2012 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/ Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 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 /> Quote Link to comment https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/#findComment-1393550 Share on other sites More sharing options...
deagle25 Posted November 19, 2012 Author Share Posted November 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/#findComment-1393557 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 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"' : ''; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/#findComment-1393567 Share on other sites More sharing options...
deagle25 Posted November 19, 2012 Author Share Posted November 19, 2012 Hi Barand Thanks for taking the time to help with this. Could you maybe post my original code with your suggested modifications? Best, Ole Quote Link to comment https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/#findComment-1393570 Share on other sites More sharing options...
deagle25 Posted November 20, 2012 Author Share Posted November 20, 2012 I found a good tutorial on the subject. http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database so just disregard my previous post, Barand. Thanks for the assistance! Quote Link to comment https://forums.phpfreaks.com/topic/270905-checkboxes-and-mysql/#findComment-1393717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.