Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to update the rank column in the users table in my database by presenting data in a PHP form and using a button to submit. However once i edit the data in my PHP form and press submit, the data in the database remains unchanged. I'm adding a (link to the) picture of the webpage, and the code is posted below. Webpage image

<!DOCTYPE HTML>
<html>
<head>
    <title>View Records</title>
</head>
<body>
<?php
/* 
    Displays all data from 'users' table
*/
    // connect to the database
    include('../db/connect.php');

    // get results from database
    $result = $MySQLi_CON->query("SELECT * FROM users") 
        or die(mysql_error());  

    // display data in table    
    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th></th></tr>";

    // loop through results of database query, displaying them in the table
    while($row = $result->fetch_array()) {
        // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['user_id'] . '</td>';
    echo '<td>' . $row['username'] . '</td>';
    echo '<td>' . $row['email'] . '</td>';
    echo '<td><input type="hidden" name="user_id[]" id="newrank" width="20px" min="0" max="100" value="' . $row['user_id'] . '"></td>';
    echo '<td><form method="POST" action=""><input type="number" name="newrank[]" id="newrank" width="20px" min="0" max="100" value="' . $row['rank'] . '"></form></td>';
    echo '<td><a href="delete.php?id=' . $row['user_id'] . '">Delete</a></td>';
    echo "</tr>"; 
} 
// close table>
echo "</table>";
if(isset($_POST['btn-update'])) {
    for($i = 0; count($_POST["user_id"]); $i++) {
        $_POST['newrank'][$i] = $MySQLi_CON->real_escape_string($_POST['newrank'][$i]); # If this function exists either, if not comment or remove this line!
    $_POST['user_id'][$i] = $MySQLi_CON->real_escape_string($_POST['user_id'][$i]); # If this function exists either, if not comment or remove this line!

    $MySQLi_CON->query('UPDATE users SET rank=' . $_POST['newrank'][$i] . ' WHERE user_id=' . $row['user_id'][$i] . '');

}
echo "Updated the rows.";
}
?>
<br>

<button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></a>
<p><a href="ny.php">Add a new record</a></p>

</body>
</html> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Seems there is an error in your query statement

Modify this : if ($$MySQLi_CON->query($sql) === TRUE) {

with if ($MySQLi_CON->query($sql) === TRUE) {


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...