[英]How to get the right select/option selected from mysql-db
I have a edit page where I can edit the status of a message. 我有一个编辑页面,可以在其中编辑消息的状态。
<select name='cm_status'>
option value='Open'>Open</option>
<option value='Closed'>Closed</option>
</select>
Then I change the status to closed, but when i go into the page again, the open is selected even when the status is set to closed, and I wonder how to get the closed one selected. 然后,我将状态更改为已关闭,但是当我再次进入页面时,即使状态设置为已关闭,也会选择打开,并且我想知道如何选择已关闭的状态。
Thanks in advance, 提前致谢,
Kristian 克里斯蒂安
You will need to query the database when the page loads and create the form after evaluating the results. 您需要在页面加载时查询数据库,并在评估结果后创建表单。
You will set the attribute selected=selected
on the option value depending on the value received from the database. 您将根据从数据库接收的值,在选项值上设置selected=selected
属性。
Here is a simple version of what you will need: 这是您需要的简单版本:
$query = mysql_query("SELECT cm_status FROM some_table");
$row = mysql_fetch_array($query)
echo "<select name='cm_status'>";
if($row == "Open")
echo "<option value='Open' selected>Open</option><option value='Closed'>Closed</option>";
else
echo "<option value='Open'>Open</option><option value='Closed' selected>Closed</option>";
echo "</select>"
This form will need a form handling PHP script that will update your MySQL database table using $sql="UPDATE mytable SET mycolumn = 1 WHERE mytable_id = 1";
该表单将需要一个处理PHP脚本的表单,该脚本将使用$sql="UPDATE mytable SET mycolumn = 1 WHERE mytable_id = 1";
来更新MySQL数据库表$sql="UPDATE mytable SET mycolumn = 1 WHERE mytable_id = 1";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.