Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Updating a mysqli table with a php form

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

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

1 Reply

0 votes
by (71.8m points)

Seems there is an error in your query statement

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...