简体   繁体   English

while循环与带有复选框php的表单不基于数据库详细信息进行更新

[英]while loop with form with checkbox php not updating based on database details

I have a while loop which is display all information correctly, I want a checkbox allowing me to mark each member of the loop as completed in the database, I don't really know where to start with this and I would appreciate any help. 我有一个while循环,可以正确显示所有信息,我希望有一个复选框允许我将循环的每个成员标记为数据库中已完成,我真的不知道从哪里开始,我将不胜感激。

Below is how far I am, it is generating the loop correctly and displaying the checkbox however it is not being set as completed on loading? 下面是我走了多远,它可以正确生成循环并显示复选框,但是未在加载时设置为已完成?

    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 action="" method="post">Completed? <input name="completed" type="checkbox" if ($task_completed == 1){checked="checked"} /><input type="submit" value="Update"><br /><br /><b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
    }
}

Any advice would be greatly appreciated 任何建议将不胜感激

You cannot write raw PHP code in a string and hope that it's executed. 您不能在字符串中编写原始PHP代码,并希望它得以执行。 Inline you also cannot use if , but you have to use the ternary operator. 内联也不能使用if ,但是必须使用三元运算符。

$tasks .= '<div id="tasksBody">
<form action="" method="post">Completed? <input name="completed" type="checkbox" '.
($task_completed == 1?'checked="checked"':'').
' /><input type="submit" value="Update">
<br /><br />
<b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM