在select中它动态显示从数据库中获取的值,我想要做的是用户从下拉列表中选择值,即实际在数据库中的值,当用户提交删除时,mysql删除所选用户为什么它不起作用?
<?php
// Database Constants
define("DB_SERVER", "localhost");
define("DB_NAME", "audit");
define("DB_USER", "root");
define("DB_PASS", "123456");
// Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// Select a database to use
mysql_select_db(DB_NAME,$connection);
?>
<html>
<head>
<title>Delete Users</title>
</head>
<body>
<?php
$username = $_POST['react'];
if(!empty($username])) {
$query= "DELETE FROM users WHERE username='$username'";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result)) {
print("<strong>$user</strong>Successfully Deleted<p>");
}
else {
print("<strong>no users are available to delete yet, sorry. </strong><p>");
}
}
?>
<form method="post" action="Delete_user.php"><div align="center"><center> <p>Delete users
<input type="hidden" name="react" value="delete_user
<select name="user" size="1">
<?php
$query = "SELECT username FROM users ORDER BY username";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result)){
//we have atleast one user,so show all users as options in select
while ($rows = mysq_fetch_row($result))
{
print("<option value=\"$rows[0]\">$rows[0]</option>");
}
}
else {
print("<option value=\"\">Please Select User</option>");
}
?>
</select><input type="submit" value="submit"></center></p></div>
</body>
</html>
答案 0 :(得分:1)
检查第二个if()
声明。
if(!empty($username])) { ...
那里有一个方括号。
接下来,在靠近底部的表单中,您有:
<input type="hidden" name="react" value="delete_user
这应该是:
<input type="hidden" name="react" value="delete_user" />
其他一切看起来还不错。尽量注意你的错误信息。
答案 1 :(得分:0)
此行中的错误
while ($rows = mysq_fetch_row($result))
更改为
while ($rows = mysql_fetch_row($result))