Rep
Rep

Reputation: 19

Got a Successful update message but nothing change in the database table

[EDITED SOLVED]

I had an update.php file to change a certain user role. I got the message update record was successful but no change in the database table. could yo specify to me where I got wrong. I'm new to Php though.

To update the username and user_role of a certain user.

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['role-dropdown']);
    $id = $_POST['id'];

    $sql = "UPDATE users SET username = '$newUsername', user_role = '$newRole' WHERE userID = '$id';"; 

if($conn->query($sql) === TRUE)
{
    echo("Record updates successfully");
}
else
{
    echo("Error updating record: ");
    die(mysqli_error($conn));
}
}

$conn->close();

?>

the webpage with the form to change the username and user-role

edit_user.php

<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" value="<?php echo $row['userID']; ?>">

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

Upvotes: 0

Views: 430

Answers (1)

Sauav
Sauav

Reputation: 180

You ID doesn't have a value

<input type="hidden" name="id">

It should be something like:

<input type="hidden" name="id" value="ID_YOU_WANT_TO_UPDATE">

Upvotes: 2

Related Questions