-1

我正在尝试更新数据库中“已完成”的值,它是一个复选框,因此我想根据数据库中的更改将值更新为 1 或 0,我知道这不是最干净的方法,但是一旦我让它工作,我会清理它!

瑞安

<?php
include 'core/init.php';

if(isset($_POST['Submit']))
{
$task_name = $_POST['task_name'];
$completed = $_POST['completed'];

mysql_query("UPDATE `tasks` SET `completed` = '$completed' WHERE `task_name` = $task_name");

}


$deets = $_POST['deets'];
$deets = preg_replace('#[^0-9/]#i', '', $deets);

$tasks = '';
$query = mysql_query('SELECT task_name, task_description, completed FROM tasks WHERE "'.$deets.'" BETWEEN `start_date` AND  `end_date` ');
$num_rows = mysql_num_rows($query);
if($num_rows > 0) {
    $tasks .= '<div id="tasksControl"><button onMouseDown="overlay()">Close</button> <b> ' . $deets . '</b> <br /><br /></div>';

    while ($row = mysql_fetch_array($query)){
        $task_name = $row['task_name'] ;
        $task_description = $row['task_description'];
        $task_completed = $row['completed'];
        $tasks .= '<div id="tasksBody">
                    <form method="post">Completed? <input name="completed" type="checkbox" '.
                    ($task_completed == 1?'checked="checked"':'').
                    ' /><input type="submit" name="submit" value="Update"><br /><br />
                    <b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br />
                    <input type="hidden" value="$task_name" name="TaskName" /></form></div>';
        }
}
echo $tasks;
?>
4

2 回答 2

0

假设已完成的列使用 0 表示未选中和 1 表示选中,您需要更改此行:

$completed = $_POST['completed'];

如果未选中该复选框,则 $_POST['completed'] 将为空/空,因为 Web 浏览器不会提交它。

$completed = intval($_POST['completed']);

usingintval()将强制为 1 或 0。

编辑:

你也应该逃避$task_name

$task_name = mysql_real_escape_string($_POST['task_name']);

我们在 SQL 中引用:

 "UPDATE tasks SET completed = '$completed' WHERE task_name = '$task_name'"
于 2013-04-19T03:37:51.677 回答
0

尝试将您的更新查询更改为:

mysql_query("UPDATE `tasks` SET `completed` = $completed WHERE `task_name` = $task_name")or die(mysql_error());

删除变量的单引号 ( ') 。$completed

于 2013-04-19T04:02:38.383 回答