0

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 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
0

or you could check

if (isset($_POST['stats3']))
  // value received
else
  // value not received

Comments

0
   <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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.