数据库无法识别下拉值[重复]

问题描述 投票:-1回答:1

我有一个编辑页面供我的用户使用输入元素和下拉列表更改他/她的角色,该下拉列表传递到更新页面。我的问题是我的下拉选择值根本无法识别..这是我的码

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()

我希望你能帮助我。

php mysql
1个回答
-1
投票

您的下拉字段名称与您尝试转义的名称不同。尝试使用“角色下拉”代替,通知应该消失。

你也试图在PHP中嵌入js,这是一个错误。尝试添加回声。 echo'warning(....)示例; “;

© www.soinside.com 2019 - 2024. All rights reserved.