0

How can I disable the checkbox and update the db using php? I mean, if the value is 1 the checkbox is checked and updated in the db, but if I unchecked the checkbox i want that in my db to be updated with 0.

$watered = $row['water'];
$i =$watered;
$check = $i == 1 ? 'checked' : '';


<input type="checkbox"  name="check" value = "$watered" <?php echo $check;?> OnClick="doAction(<?php echo $id;?>);" > </td>

the function doAction who doesn't matter and the file where is gone:

  if (isset($_GET['id'])) {

        $id = $_GET['id'];
        $query = mysql_query("update plants set watered = 1 where id = $id ");
        if ($query){
            echo "you have watered the plants.";
        }else {
            echo "you cannot watered the plants";
        }
    }else{
        echo "vvvvvvvvv";
}
2
  • I tried to echo $_GET['check'] but didn't print anything Commented Jul 27, 2016 at 7:05
  • You can do get checkbox value, so It will return all selected checkbox so update it to database and then Select all value which is not equal to your selected checkbox , So you got all unselected checkbox, Now set value 0 for that all. Commented Jul 27, 2016 at 7:33

1 Answer 1

1

Try this

$check = $row['water'] == 1 ? 'checked' : '';

<input type="checkbox"  name="check" value = "<?php echo  $row['water']; ?>" <?php echo $check;?> OnClick="doAction(<?php echo $id;?>);" > </td>

And

if (isset($_GET['id'])) {

  $id = mysql_real_escape_string($_GET['id']); //some security...
  $check = mysql_real_escape_string($_GET['check']); //some security...
        $query = mysql_query("update plants set watered = ".$check." where id = ".$id);
        if ($query){
            echo "you have watered the plants.";
        }else {
            echo "you cannot watered the plants";
        }
    }else{
        echo "vvvvvvvvv";
}

Sorry, it's really hard to understand you code.... Please do everything with PDO or mysqli, mysql_* functions are deprecated. Is it working?

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

2 Comments

not really. i don't get it. why did you change the argument of the doAction function from $id with $row[...]?
sorry, I have seen in your code that $i==$watered==$row['water'] and I read that $id as $i. Fixed. Sorry about that :)

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.