Nightkon Posted April 5, 2008 Share Posted April 5, 2008 Hi All, I have a basic Checkbox in PHP which saves to a MYSQL database. I am able to create a Checkbox which correctly populates from the MYSQL table. The issue is that when a user clicks on the save button the information is not saved back to the database correctly. Basically if you check the boxes and save the database record is set to 0 (unchecked) and the checkboxes return to an unchecked state. I suspect that the code is unable to determine if the checkbox is checked when it saves as there are no errors being returned during the update process. The Table is called car_checkbox and has the fields FileSerial (Primary Key), year_2006 and year_2007. The .php files I am using are TESTPHPCHECK.php which contains the code used for updating the Checkbox. TESTCheckbox.php which holds the form and table. CAR_Checkbox which is the code for the checkbox itself and populates the values stored in the table. If anyone has any solutions or advice it would be very much appreciated. Thanks in advance. CAR_checkbox.php <?php include("mysql_connect.php"); $q="SELECT * FROM `car_checkbox` WHERE `FileSerial` = '$id'"; $r=mysql_query($q); $num=mysql_numrows($r); if ($num>0) { $val1=mysql_result($r,'0','year_2006'); $val2=mysql_result($r,'0','year_2007'); } else { //Else nothing is checked $val1=0; $val2=0; } //Show form echo "<input type='hidden' name='id' value='$id'>"; if ($val1=="0") { echo "<input name='year_2006' type='checkbox' value='year_2006'>2006"; } else { echo "<input checked='checked' input name='year_2006' type='checkbox' value='year_2006'>2006"; } if ($val2=="0") { echo "<input name='year_2007' type='checkbox' value='year_2007'>2007"; } else { echo "<input checked='checked' input name='year_2007' type='checkbox' value='year_2007'>2007"; } ?> TESTCheckbox.php I believe the code at the start of TESTCheckbox.php may have something to do with the issue but I have not been able to work it out. <?php include ('./populate.php'); ?> <?php $id=$_GET['id']; $val1=$_GET['year_2006']; if ($val1=="on") { $val1=1; } else { $val1=0; } $val2=$_GET['year_2007']; if ($val2=="on") { $val2=1; } else { $val2=0; } ?> <table width='1044' align="left" cellpadding="0" cellspacing="0" > <form action="TESTPHPCHECK.php?id=$id" method="POST" name='theForm' onsubmit=''> <input type="hidden" name="id" value="<?php echo $id; ?>"> <!--DWLayoutTable--> <td height="117" colspan="17" valign="top"><INPUT name="image" TYPE="image" class='forminput' value="Save" SRC="Buttons/NavBanner.gif" /> <h3><?php echo $row->current_name; ?></h3></td> <td width="11"> </td> <tr> <td height="13"></td> <td></td> <td colspan="5" rowspan="2" valign="top" class="edit">CAR Recd. </td> <td rowspan="2" align="center" valign="top"><?php include ('CAR_checkbox.php'); ?></td> <td></td> <td></td> <td></td> <td></td> </tr> </form> </table> TESTPHPCHECK.php <?php include("mysql_connect.php"); $val1 = $_POST['year_2006']; $val2 = $_POST['year_2007']; $id = $_POST['id']; $query17 = mysql_query("UPDATE car_checkbox SET year_2006='$val1' WHERE FileSerial='$id'") or die(mysql_error()); $query18 = mysql_query("UPDATE car_checkbox SET year_2007='$val2' WHERE FileSerial='$id'") or die(mysql_error()); ## The above statements run the whole query ## tyr adding some error checking if(!$query17){ echo "There is an error with query 17 <br>"; } else { echo "Query 17 was successful<br>"; } if(!$query18){ echo "There is an error with query 18 <br>"; } else { echo "Query 18 was successful<br>"; } #for more error checking you can try checking your posted variables with print_r($_POST); mysql_close($link); ?> Quote Link to comment Share on other sites More sharing options...
hellbringer Posted April 5, 2008 Share Posted April 5, 2008 echo "<input checked='checked' input name='year_2006' type='checkbox' value='year_2006'>2006"; Quote Link to comment Share on other sites More sharing options...
Nightkon Posted April 5, 2008 Author Share Posted April 5, 2008 Still have the same problem. Thanks though Hellbringer Quote Link to comment Share on other sites More sharing options...
Nightkon Posted April 6, 2008 Author Share Posted April 6, 2008 I also noticed that if you change the code "if ($val1=="0") {" in the CAR_checkbox file to "if ($val1=="1") {" the checkbox is always checked when the save button is clicked. Quote Link to comment Share on other sites More sharing options...
Nightkon Posted May 18, 2008 Author Share Posted May 18, 2008 Does anyone have any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 18, 2008 Share Posted May 18, 2008 yeah $num=mysql_numrows($r); the alias is msql_numrows()... Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 18, 2008 Share Posted May 18, 2008 http://uk3.php.net/manual-lookup.php?pattern=mysql_numrows&lang=en Quote Link to comment Share on other sites More sharing options...
Nightkon Posted May 25, 2008 Author Share Posted May 25, 2008 Thanks Marklarah. I am now using the correct function but the checkbox still does not save properly ??? Thanks for your help though I must be getting close Quote Link to comment Share on other sites More sharing options...
Darklink Posted May 25, 2008 Share Posted May 25, 2008 The value of year_2006/7 will not be "on". If checked it will be "year_2007" because thats what you set the value too. If you want to have it set to true or false, you want to do: <input type="checkbox" name="year_2007" value="1" /> Then in your PHP: <?php if ( $_POST['year_2007'] ) { echo("Year 2007 was selected"); } else { echo("Year 2007 was NOT selected"); } ?> Quote Link to comment Share on other sites More sharing options...
Nightkon Posted June 1, 2008 Author Share Posted June 1, 2008 Thanks Darklink. I am now able to tell if the checkbox is checked or not based on the changes you suggested. The only issue now is getting the '1' or the '0' to save back to the database correctly. I have tried altering the update PHPCHECK.php file which is supposed to update the database but have had no luck. Any help would be appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
whizard Posted June 1, 2008 Share Posted June 1, 2008 Why is TESTCheckbox.php looking for GET['year2006/7'] to equal "on"? In other words, where is "on" coming from? Dan Quote Link to comment Share on other sites More sharing options...
Nightkon Posted June 1, 2008 Author Share Posted June 1, 2008 Not sure exactly Whizard it was part of the original code that I was working with. I have been leaving that whole section of code out and it doesn't make any difference. Quote Link to comment Share on other sites More sharing options...
Nightkon Posted June 8, 2008 Author Share Posted June 8, 2008 I know that this is streching on a bit but any additional help or direction would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
sasa Posted June 8, 2008 Share Posted June 8, 2008 CAR_checkbox.php <?php include("mysql_connect.php"); $q="SELECT * FROM `car_checkbox` WHERE `FileSerial` = '$id'"; $r=mysql_query($q); $num=mysql_numrows($r); if ($num>0) { $val1=mysql_result($r,'0','year_2006'); $val2=mysql_result($r,'0','year_2007'); } else { //Else nothing is checked $val1=0; $val2=0; } //Show form echo "<input type='hidden' name='id' value='$id'>"; echo '<input type="hidden" name="year_2006" value="0">'; if ($val1=="0") { echo "<input name='year_2006' type='checkbox' value='1'>2006"; } else { echo "<input checked='checked' input name='year_2006' type='checkbox' value='1'>2006"; } echo '<input type="hidden" name="year_2007" value="0">'; if ($val2=="0") { echo "<input name='year_2007' type='checkbox' value='1'>2007"; } else { echo "<input checked='checked' input name='year_2007' type='checkbox' value='1'>2007"; } ?> Personal Message (Offline) Checkbox not saving correctly to MYSQL table « on: 05-04-2008, 17:07:36 » Reply with quoteQuote Hi All, I have a basic Checkbox in PHP which saves to a MYSQL database. I am able to create a Checkbox which correctly populates from the MYSQL table. The issue is that when a user clicks on the save button the information is not saved back to the database correctly. Basically if you check the boxes and save the database record is set to 0 (unchecked) and the checkboxes return to an unchecked state. I suspect that the code is unable to determine if the checkbox is checked when it saves as there are no errors being returned during the update process. The Table is called car_checkbox and has the fields FileSerial (Primary Key), year_2006 and year_2007. The .php files I am using are TESTPHPCHECK.php which contains the code used for updating the Checkbox. TESTCheckbox.php which holds the form and table. CAR_Checkbox which is the code for the checkbox itself and populates the values stored in the table. If anyone has any solutions or advice it would be very much appreciated. Thanks in advance. CAR_checkbox.php Code: <?php include("mysql_connect.php"); $q="SELECT * FROM `car_checkbox` WHERE `FileSerial` = '$id'"; $r=mysql_query($q); $num=mysql_numrows($r); if ($num>0) { $val1=mysql_result($r,'0','year_2006'); $val2=mysql_result($r,'0','year_2007'); } else { //Else nothing is checked $val1=0; $val2=0; } //Show form echo "<input type='hidden' name='id' value='$id'>"; if ($val1=="0") { echo "<input name='year_2006' type='checkbox' value='year_2006'>2006"; } else { echo "<input checked='checked' input name='year_2006' type='checkbox' value='year_2006'>2006"; } if ($val2=="0") { echo "<input name='year_2007' type='checkbox' value='year_2007'>2007"; } else { echo "<input checked='checked' input name='year_2007' type='checkbox' value='year_2007'>2007"; } ?> TESTCheckbox.php I believe the code at the start of TESTCheckbox.php may have something to do with the issue but I have not been able to work it out. Code: <?php include ('./populate.php'); ?> <?php $id=$_GET['id']; $val1=$_GET['year_2006']; if ($val1=="on") { $val1=1; } else { $val1=0; } $val2=$_GET['year_2007']; if ($val2=="on") { $val2=1; } else { $val2=0; } ?> <table width='1044' align="left" cellpadding="0" cellspacing="0" > <form action="TESTPHPCHECK.php?id=$id" method="POST" name='theForm' onsubmit=''> <input type="hidden" name="id" value="<?php echo $id; ?>"> <!--DWLayoutTable--> <td height="117" colspan="17" valign="top"><INPUT name="image" TYPE="image" class='forminput' value="Save" SRC="Buttons/NavBanner.gif" /> <h3><?php echo $row->current_name; ?></h3></td> <td width="11"> </td> <tr> <td height="13"></td> <td></td> <td colspan="5" rowspan="2" valign="top" class="edit">CAR Recd. </td> <td rowspan="2" align="center" valign="top"><?php include ('CAR_checkbox.php'); ?></td> <td></td> <td></td> <td></td> <td></td> </tr> </form> </table> TESTPHPCHECK.php <?php include("mysql_connect.php"); $val1 = $_POST['year_2006']; $val2 = $_POST['year_2007']; $id = $_POST['id']; $query17 = mysql_query("UPDATE car_checkbox SET year_2006='$val1' WHERE FileSerial='$id'") or die(mysql_error()); $query18 = mysql_query("UPDATE car_checkbox SET year_2007='$val2' WHERE FileSerial='$id'") or die(mysql_error()); ## The above statements run the whole query ## tyr adding some error checking if(!$query17){ echo "There is an error with query 17 <br>"; } else { echo "Query 17 was successful<br>"; } if(!$query18){ echo "There is an error with query 18 <br>"; } else { echo "Query 18 was successful<br>"; } #for more error checking you can try checking your posted variables with print_r($_POST); mysql_close($link); ?> Quote Link to comment 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.