Ell20 Posted December 8, 2007 Share Posted December 8, 2007 Hi, I have a simple checkbox for the user to say whether or not their club has a pavilion. Once this has been stored I would like the page to check the database to see whether or not the checkbox should be checked. I tried <input type="checkbox" name="pavilion" value="<?=$pavilion?>" /> But this didnt work, is there anyway I can store a simple "Yes" to the database once ticked and then make the box ticked each time the user views the page? Thanks Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/ Share on other sites More sharing options...
pocobueno1388 Posted December 8, 2007 Share Posted December 8, 2007 It would be something like this <?php if (isset($_POST['submit'])){ //check if they checked that they have a pavilion if (isset($_POST['pavilion'])){ //update the database with "yes" $query = mysql_query("UPDATE table SET pavilion='yes' WHERE pavilion='{$_POST['pavilion']}'")or die(mysql_error()); } } //check the databse to figure out if the field should be checked $query = mysql_query("SELECT pavilion FROM table WHERE pavilion='$pavilion'")or die(mysql_error()); $row = mysql_fetch_assoc($query); if ($row['pavilion'] == 'yes') $checked = "checked='yes'"; else $checked = ""; ?> <form method="post"> <input type="checkbox" name="pavilion" value="<?=$pavilion?>" <?=$checked?>/><br> <input type="submit" name="submit"> </form> Just edit the database information and see if it works for you. Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409924 Share on other sites More sharing options...
Ell20 Posted December 8, 2007 Author Share Posted December 8, 2007 Ok, I tried that on its own and of course, its works, thanks. But how do I go about doing this for say 10 check boxes, as thats how many I have on the page? Thanks Elliot Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409929 Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Each $row array will have the names in them, so just do the same thing for each name. Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409931 Share on other sites More sharing options...
Ell20 Posted December 8, 2007 Author Share Posted December 8, 2007 I have managed to do it for all 10 now and it works at first however, if for some reason any of the checkboxes need to be unticked then submitted again the check box is not updating and stays checked regardless? Thanks Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409957 Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Makes sense since here //check if they checked that they have a pavilion if (isset($_POST['pavilion'])){ //update the database with "yes" $query = mysql_query("UPDATE table SET pavilion='yes' WHERE pavilion='{$_POST['pavilion']}'")or die(mysql_error()); } is checking if its set, if its not set (unchecked), the update query gets skipped. Put an else in there to set it to "No" if its not set. Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409961 Share on other sites More sharing options...
Ell20 Posted December 8, 2007 Author Share Posted December 8, 2007 Like this: if (isset($_POST['submit3'])){ if (isset($_POST['pavilion'])){ $query = mysql_query("UPDATE club SET pavilion='yes' WHERE pavilion='{$_POST['pavilion']}' AND club_id='$id'")or die(mysql_error()); } else { $query = mysql_query("UPDATE club SET pavilion='no' WHERE pavilion='{$_POST['pavilion']}' AND club_id='$id'")or die(mysql_error()); } } Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409974 Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Give it a try Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409976 Share on other sites More sharing options...
Ell20 Posted December 8, 2007 Author Share Posted December 8, 2007 Gives this error if I try and untick it and submit again? An error occured in script /home/mysport/public_html/aboutclub.php on line 266: Undefined index: pavilion Thanks Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409982 Share on other sites More sharing options...
Ell20 Posted December 8, 2007 Author Share Posted December 8, 2007 Fixed I think, thanks for your help! Had to remove WHERE pavilion = POST....... from the No part! Thanks Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409986 Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Probably dont need this anymore WHERE pavilion='{$_POST['pavilion']} for the no line Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409987 Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Right on Link to comment https://forums.phpfreaks.com/topic/80809-solved-updateedit-database-using-checkbox/#findComment-409988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.