我正在尝试更新数据库中“completed”的值,它是一个复选框,所以我想根据数据库中的更改将值更新为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;
?>
答案 0 :(得分:0)
假设已完成的列使用0表示未选中,1表示已选中,则需要更改此行:
$completed = $_POST['completed'];
如果取消选中该复选框,$ _POST ['completed']将为空/空,因为网络浏览器不会提交。
$completed = intval($_POST['completed']);
使用intval()
将强制1或0。
编辑:
你也应该逃避$task_name
:
$task_name = mysql_real_escape_string($_POST['task_name']);
我们在SQL中引用:
"UPDATE tasks SET completed = '$completed' WHERE task_name = '$task_name'"
答案 1 :(得分:0)
尝试将更新查询更改为:
mysql_query("UPDATE `tasks` SET `completed` = $completed WHERE `task_name` = $task_name")or die(mysql_error());
删除单引号('
)到$completed
变量。