如何禁用复选框并使用php更新数据库

时间:2016-07-27 07:00:23

标签: php mysql checkbox

如何禁用复选框并使用php更新数据库?我的意思是,如果值为1,则在数据库中检查并更新复选框,但是如果我取消选中该复选框,我希望我的数据库中的复选框用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>

函数doAction谁不重要,文件哪里不见了:

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

1 个答案:

答案 0 :(得分:1)

试试这个

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

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

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

很抱歉,您很难理解您的代码....请使用PDO或mysqli执行所有操作,mysql_ *函数已弃用。它有效吗?