Jump to content

Checkbox not saving correctly to MYSQL table


Nightkon

Recommended Posts

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);
?> 

 

Link to comment
Share on other sites

  • 1 month later...

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");
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.