我确实有一个编辑页面供用户使用输入元素和下拉列表更改其角色,该下拉列表将传递给更新页面。我的问题是我的下拉选择值根本无法识别。 。这是我的代码
edit_user.php
<?php
require 'includes/dbconnect.php';
if(isset($_GET['edit']))
{
$id = $_GET['edit'];
$sql = "SELECT * FROM users;";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Account Edit and View</title>
<meta charset="utf-8" name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/edit_user_page.css">
</head>
<body>
<header id="gt-header">
<img id="gt-image" src="Gabtech-Global.png" alt="Gabtech Global logo" width = "310" height="80">
</header>
<main>
<div id="gt-menu">
</div>
<form id="user-form" class="form" method="POST" action="includes/update.php">
<h3>Edit User Role</h3>
<label for="username">Username:</label>
<input id="username" type="text" name="username" value="<?php echo $row['username']; ?>">
<input type="hidden" name="id">
<select class="select" id="role-dropdown" name="role-dropdown">
<option value="_Any" selected="selected">Please Choose</option>
<option value="1">Admin</option>
<option value="2">Manager</option>
<option value="3">General User</option>
<option value="4">External Company</option>
</select>
<button type="submit" name="update-btn" id="update-btn">Update</button>
</form>
</main>
<?php
require 'includes/footer.php';
?>
</body>
</html>
update.php
<?php
require 'dbconnect.php';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$newUsername = mysqli_real_escape_string($conn, $_POST['username']);
$newRole = mysqli_real_escape_string($conn, $_POST['user_role']);
$id = $_POST['id'];
$sql = "UPDATE users SET username = '$newUsername', user_role = '$newRole' WHERE userID = $id;";
if($conn->query($sql) === TRUE)
{
alert("Record updates successfully");
}
else
{
alert("Error updating record: ");
}
}
$conn->close();
?>
,我得到一个错误: 注意:未定义的索引:第7行的D:\ xampp \ htdocs \ gabtech_crm \ includes \ update.php中的user_role
致命错误:在第18行的D:\ xampp \ htdocs \ gabtech_crm \ includes \ update.php中调用未定义的函数alert()
我希望你能帮我这个忙。
答案 0 :(得分:-1)
您的下拉字段名称与您尝试转义的名称不同。尝试改为使用“角色下拉列表”,通知应该消失了。
另外,您正在尝试将js嵌入PHP中,这是一个错误。尝试添加回声。 示例echo'alert(....); ';