使用php表单更新mysqli表

时间:2016-03-30 10:27:27

标签: php forms mysqli

我试图通过在PHP表单中显示数据并使用按钮提交来更新数据库中rank表中的users列。但是,一旦我在PHP表单中编辑数据并按提交,数据库中的数据将保持不变。我正在添加一个(链接到)网页的图片,代码发布在下面。 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> 

2 个答案:

答案 0 :(得分:0)

似乎您的query声明

中存在错误

修改:if ($$MySQLi_CON->query($sql) === TRUE) {

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

答案 1 :(得分:0)

您需要将要修改的ID解析为$ _POST。此外,您需要在代码中使用<form action="" method="POST">

未测试过,但以下情况应该有效:

<!DOCTYPE HTML>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border='1' cellpadding='10'>
<thead> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th colspan="3"></th></thead>
<tbody>

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

// loop through results of database query, displaying them in the table
while($row = $result->fetch_assoc()) { 
    // echo out the contents of each row into a table
    ?>
   <form action="" method="POST">
       <tr>
           <td><?php echo $row['user_id']; ?></td>
           <td><?php echo $row['username']; ?></td>
           <td><?php echo $row['email'];?></td>
           <td><input type="number" name="newrank" id="newrank" value="<?php  echo $row['rank']; ?>"></td>        
           <td><input type="hidden" name="id" value="<?php echo $row['user_id']; ?>"><button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></td>
           <td><a href="delete.php?id=<?php echo $row['user_id']; ?>">Delete</a></td>
       </tr>
   </form>
   <?php
} 
?>

</tbody>
</table>
<?php 

if(isset($_POST['btn-update']))
{
$sql = 'UPDATE users SET rank=' . $_POST['newrank'] . ' WHERE user_id=' . $_GET['id'] . '';

if ($MySQLi_CON->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $MySQLi_CON->error;
}
}
?>

<p><a href="ny.php">Add a new record</a></p>

</body>
</html>