tags:

views:

63

answers:

4

Why can't the data in mysql not updated when I use checkbox, I already tried removing the input type hidden, but still didn't work. I think the problem must be in this part, because when I try to add records and check all of the checkboxes. Then I will try to update it. By removing the checks in the other entry. The data is updated. the checks that are removed are also reflected in the database, but when I try to add checks on them again using the update module, it will not be updated. Please help, I'm just a beginner.

<td><input name="stats3" type="checkbox" id="sh"
 value="<?php echo $row["STAT3"]; ?>" <?php echo $row["STAT3"] ? 'checked="checked"' : ''; ?> >Stockholder</td>
+3  A: 

When you have a checkbox and you tick it, you get a key/value pair returned in the post to the server.

When the checkbox is not checked, it actually doesn't come back in the post (you can check this using Firebug, or by print_r($_POST)

This could be causing your problem.

You can either use:

if(isset($_POST['stats3'])) {
   // checked is true
}

Or another easy solution is to have a select list with yes and no as options, which will always give you a value back - otherwise you need to set and unset the value based on the presence of the tick box in the post.

Sohnee
I disagree a lot with the idea of using a select list because it is easier to code. The UI/UX shouldn't be compromised in this way.
Richard Harrison
To be fair, I did also point out how to determine whether the checkbox was ticked too. I don't think it's bad to give alternatives.
Sohnee
+3  A: 

Duplicate of this answer -

Use isset($_POST['stats3']) to determine if a checkbox is ticked or not.

Richard Harrison
you got there right before me :)
espais
to ensure things aren't being messed with, I instead check if($_POST['stats3'] == 'checked'){ echo ' checked="checked"';} , as this is what the post value states if you look at it.
Cryophallion
@Cryophallion you should still use an isset to ensure that the variable is in the $_POST to avoid PHP warnings.I also don't think it helps at all to prevent "things being messed" with - but it is fair to say that your way is a more complete test of validity.
Richard Harrison
You are correct, and a quick check of my checkbox object shwos that I did do an isset first. Should have checked my code first... Post is less likely to get tweaked, but I just do it as a paranoid thing for occasions when the form is public facing. Of course, I still validate before doing anything, but I just wanted to throw in my .02
Cryophallion
A: 

or you could check

if (isset($_POST['stats3']))
  // value received
else
  // value not received
espais
A: 
   <input name="stats3" type="checkbox" id="sh"
     value="
       <?php echo $row["STAT3"]; ?>
     " 
       <?php echo $row["STAT3"] ? 'checked="checked"' : ''; ?>
    >Stockholder

Its just working fine for me,

are if you are posting multiple values in the same name as "stats3" then you will have a problem

Awwam