Jump to content

checkboxes in form are not inserting to mysql


turpentyne

Recommended Posts

I built a series of insertion forms to put entries into a mysql database. Everything is working fine, except that I just now realized my checkboxes don't put any entry in. It's just leaving it's respective smallint field as a default "0". Being new, I've must've overlooked how to handle these.

 

The form's checkboxes have a bit of code in the value that remembers what users chose in case they have to backtrack and redo their form. How would I solve the problem and keep this code? Here's what all the checkboxes are written like:

 

<input type="checkbox" name="green_leaf" value="<?php if(isset($_POST['1'])) echo $_POST['1']; ?>" > green

 

 

 

 

Link to comment
Share on other sites

The thing about checkboxes is they are only passed if they are checked,

 

so your code should look like this

green_leaf was 
<?php
if(isset($_POST['green_leaf'])){
    echo "checked";
}else{
    echo "NOT checked";
}
?>
<input type="checkbox" name="green_leaf" value="<?php if(isset($_POST['green_leaf'])) echo 'checked="checked"' ?>" > green

 

Hope that helps

Link to comment
Share on other sites

hmmm. Didn't work. I wonder if there's something more I'm missing here?

 

I stripped away all the other database items in the form, and this is essentially what I've got:


$descriptor60 = trim($_POST['green_leaf']);
$query = "INSERT INTO tablename (green_leaf) VALUES ('$descriptor60')";

<input type="checkbox" name="green_leaf" value="<?php if(isset($_POST['green_leaf'])) {echo 'checked="checked"';} ?>" >

 

in the mysql table, the green_leaf field is: tinyint, set to default of 0 and not null.

 

any thoughts? I've tried a couple variations and still no luck. The checkboxes remain as zeros

 

Link to comment
Share on other sites

Sorry for the delay - been working other projects...

 

Here's a bit more code. It's quite a lengthy page, so I've still let out numerous irrelevant variables and form items.

 


<html>
<title>submit a new plant </title>
<link rel="stylesheet" type="text/css" href="styles.css"> 
<body>


<?php

if (isset($_POST['submitted'])) {
$errors = array();

$descriptor1 = trim($_POST['plant_id']);

if (empty($_POST['about_this_plant'])) {
$errors[] = 'your plant id did not continue to the new page';

} else {
$descriptor2 = trim($_POST['about_this_plant']);
}
$descriptor60 = trim($_POST['green_leaf']);
$descriptor61 = trim($_POST['white_leaf']);
$descriptor62 = trim($_POST['cream_leaf']);
$descriptor63 = trim($_POST['pink_leaf']);
$descriptor64 = trim($_POST['red_leaf']);
$descriptor65 = trim($_POST['orange_leaf']);
$descriptor66 = trim($_POST['yellow_leaf']);
$descriptor67 = trim($_POST['blue_leaf']);
$descriptor68 = trim($_POST['purple_leaf']);
$descriptor69 = trim($_POST['purpleblack_leaf']);
if (empty($errors)) {

require ('databaseConnectFile.php');

$query = "INSERT INTO table (green_leaf, white_leaf, cream_leaf, pink_leaf, red_leaf, orange_leaf, yellow_leaf, blue_leaf, purple_leaf, purpleblack_leaf)
VALUES ('$descriptor60', '$descriptor61', '$descriptor62', '$descriptor63', '$descriptor64', '$descriptor65', '$descriptor66', '$descriptor67', '$descriptor68', '$descriptor69')";

$result = @mysql_query ($query);
if ($result) {

if(isset($_POST['about_this_plant']))
{
$plant_id=mysql_insert_id();
}

header ("Location: http://www.mywebsite.com/nextpage.php?var1=$plant_id");


echo 'one plant has been added';

exit();
} else {
echo 'system error. No plant added';

echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';

exit();

}
mysql_close();

} else {

echo 'error. the following error occured <br>';
foreach ($errors as $msg) {

echo " - $msg<br>\n";

}

} // end of if

} // end of main submit conditional

?>

<FORM style="border: 1px dotted red; padding: 2px" action="nextpage.php" method="post"><fieldset><legend><b>Describe your plant</b></legend>
Leaf color (not including fall season):<br>
<input type="checkbox" name="green_leaf" value="<?php if(isset($_POST['green_leaf'])) {echo 'checked="checked"';} ?>" > green            
<input type="checkbox" name="white_leaf" value="<?php if(isset($_POST['white_leaf'])) {echo 'checked="checked"';} ?>" > white     
<input type="checkbox" name="cream_leaf" value="<?php if(isset($_POST['cream_leaf'])) {echo 'checked="checked"';} ?>" > cream<br>
<input type="checkbox" name="pink_leaf" value="<?php if(isset($_POST['pink_leaf'])) {echo 'checked="checked"';} ?>" > pink             
<input type="checkbox" name="red_leaf" value="<?php if(isset($_POST['red_leaf'])) {echo 'checked="checked"';} ?>" > red        
<input type="checkbox" name="orange_leaf" value="<?php if(isset($_POST['orange_leaf'])) {echo 'checked="checked"';} ?>" > orange<br>
<input type="checkbox" name="yellow_leaf" value="<?php if(isset($_POST['yellow_leaf'])) {echo 'checked="checked"';} ?>" > yellow         
<input type="checkbox" name="blue_leaf" value="<?php if(isset($_POST['blue_leaf'])) {echo 'checked="checked"';} ?>" > blue       
<input type="checkbox" name="purple_leaf" value="<?php if(isset($_POST['purple_leaf'])) {echo 'checked="checked"';} ?>" > purple<br>
<input type="checkbox" name="purpleblack_leaf" value="<?php if(isset($_POST['purpleblack_leaf'])) {echo 'checked="checked"';} ?>" > purple-black<br>
<input type="hidden" name="submitted" value="TRUE">
<input type="hidden" name="submitted_forward" value='$sn'>
<input type="submit" />

</form>
</body>
</html>

Link to comment
Share on other sites

Sorry i just noticed a type in the html,

however plant_id isn't in the form !! and a header is after output!

 

here is a full update

<html>
  <title>submit a new plant </title>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <body>
    <?php
    if (isset($_POST['submitted'])) {
      $errors = array();
      $descriptor1 = trim($_POST['plant_id']);
      if (empty($_POST['about_this_plant'])) {
$errors[] = 'your plant id did not continue to the new page';
      } else {
$descriptor2 = trim($_POST['about_this_plant']);
      }
      $descriptor60 = isset($_POST['green_leaf']);
      $descriptor61 = isset($_POST['white_leaf']);
      $descriptor62 = isset($_POST['cream_leaf']);
      $descriptor63 = isset($_POST['pink_leaf']);
      $descriptor64 = isset($_POST['red_leaf']);
      $descriptor65 = isset($_POST['orange_leaf']);
      $descriptor66 = isset($_POST['yellow_leaf']);
      $descriptor67 = isset($_POST['blue_leaf']);
      $descriptor68 = isset($_POST['purple_leaf']);
      $descriptor69 = isset($_POST['purpleblack_leaf']);
      if (empty($errors)) {
require ('databaseConnectFile.php');
$query = "INSERT INTO table (green_leaf, white_leaf, cream_leaf, pink_leaf, red_leaf, orange_leaf, yellow_leaf, blue_leaf, purple_leaf, purpleblack_leaf)
VALUES ('$descriptor60', '$descriptor61', '$descriptor62', '$descriptor63', '$descriptor64', '$descriptor65', '$descriptor66', '$descriptor67', '$descriptor68', '$descriptor69')";

$result = @mysql_query ($query);
if ($result) {
  if(isset($_POST['about_this_plant'])) {
    $plant_id=mysql_insert_id();
  }
  header ("Location: http://www.mywebsite.com/nextpage.php?var1=$plant_id");
  echo 'one plant has been added';
  exit();
} else {
  echo 'system error. No plant added';
  echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';
  exit();
}
mysql_close();
      } else {
echo 'error. the following error occured <br>';
foreach ($errors as $msg) {
  echo " - $msg<br>\n";
}
      } // end of if
    } // end of main submit conditional
    ?>
    <form style="border: 1px dotted red; padding: 2px" action="nextpage.php" method="post"><fieldset><legend><b>Describe your plant</b></legend>
Leaf color (not including fall season):<br>
<input type="checkbox" name="green_leaf" value="" <?php if(isset($_POST['green_leaf'])) echo 'checked="checked"'; ?> > green          
<input type="checkbox" name="white_leaf" value="" <?php if(isset($_POST['white_leaf'])) echo 'checked="checked"'; ?> > white     
<input type="checkbox" name="cream_leaf" value="" <?php if(isset($_POST['cream_leaf'])) echo 'checked="checked"'; ?> > cream<br>
<input type="checkbox" name="pink_leaf" value="" <?php if(isset($_POST['pink_leaf'])) echo 'checked="checked"'; ?> > pink            
<input type="checkbox" name="red_leaf" value="" <?php if(isset($_POST['red_leaf'])) echo 'checked="checked"'; ?> > red        
<input type="checkbox" name="orange_leaf" value="" <?php if(isset($_POST['orange_leaf'])) echo 'checked="checked"'; ?> > orange<br>
<input type="checkbox" name="yellow_leaf" value="" <?php if(isset($_POST['yellow_leaf'])) echo 'checked="checked"';?> > yellow         
<input type="checkbox" name="blue_leaf" value="" <?php if(isset($_POST['blue_leaf'])) echo 'checked="checked"';?> > blue       
<input type="checkbox" name="purple_leaf" value="" <?php if(isset($_POST['purple_leaf'])) echo 'checked="checked"';?> > purple<br>
<input type="checkbox" name="purpleblack_leaf" value="" <?php if(isset($_POST['purpleblack_leaf'])) echo 'checked="checked"';?> > purple-black<br>
<input type="hidden" name="submitted" value="TRUE">
<input type="hidden" name="submitted_forward" value='$sn'>
<input type="submit" />
    </form>
  </body>
</html>

Link to comment
Share on other sites

this:

<input type="checkbox" name="green_leaf" value="<?php if(isset($_POST['green_leaf'])) {echo 'checked="checked"';} ?>" > green

does not seem right

 

It should be:

<input type="checkbox" name="green_leaf" value="1" <?php if(isset($_POST['green_leaf'])) {echo 'checked="checked"';} ?> > green

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.