用php表单更新一个mysqli表 [英] Updating a mysqli table with a php form

查看:135
本文介绍了用php表单更新一个mysqli表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <!DOCTYPE HTML> 
< html>
< head>
< title>查看记录< / title>
< / head>
< body>
<?php
/ *
显示'users'表中的所有数据
* /
//连接到数据库
include('。 ./db/connect.php');

//从数据库中获取结果
$ result = $ MySQLi_CON-> query(SELECT * FROM users)
或die(mysql_error());

//在表格中显示数据
echo< table border ='1'cellpadding ='10'>;
echo< tr>第ID>第< th>第< th>第< th>用户第< th>第< th> < /第>< / TR>中;

//循环查看数据库查询的结果,在表中显示它们
while($ row = $ result-> fetch_array()){
// echo out每行的内容放到一个表中
echo< tr>;
回声'< td>'。 $ row ['user_id']。 < / TD>;
回声'< td>'。 $ row ['username']。 < / TD>;
回声'< td>'。 $ row ['email']。 < / TD>;
echo'< td>< input type =hiddenname =user_id []id =newrankwidth =20pxmin =0max =100value ='。 $ row ['user_id']。'>< / td>';
echo'< td>< form method =POSTaction =>< input type =numbername =newrank []id =newrankwidth =20pxmin =0max =100value ='。$ row ['rank']。'>< / form>< / td>';
echo'< td>< a href =delete.php?id ='。$ row ['user_id']。'>删除< / a>< / td>';
回声< / tr>;
}
//关闭表格>
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]); #如果这个函数存在,如果没有注释或者删除这行!
$ _POST ['user_id'] [$ i] = $ MySQLi_CON-> real_escape_string($ _ POST ['user_id'] [$ i]); #如果这个函数存在,如果没有注释或者删除这行!

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

}
echo更新了行。;
}
?>
< br>

< button type =submitclass =btn btn-defaultname =btn-updateid =btn-update>更新< / button>< / a>
< p>< a href =ny.php>添加新记录< / a>< / p>

< / body>
< / html>


解决方案

你需要解析你想修改的id $ _POST。此外,您需要在代码中使用< form action =method =POST>

没有测试过它,但以下内容可以正常工作:

 <!DOCTYPE HTML> 
< html>
< head>
< title>查看记录< / title>
< / head>
< body>
< table border ='1'cellpadding ='10'>
< thead> <的第i; ID< /第> <的第i;用户名和LT; /第> <的第i;电子邮件与LT; /第> <的第i;秩< /第> < th colspan =3>< / th>< / thead>
< tbody>

<?php
//显示'users'表中的所有数据
//连接到数据库
include('../ db / connect。 PHP');

//从数据库中获取结果
$ result = $ MySQLi_CON-> query(SELECT * FROM users)
或die(mysql_error());

//循环查看数据库查询的结果,在表中显示它们
while($ row = $ result-> fetch_assoc()){
// echo out每行内容放入表格
?>
< 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 =numbername =newrankid =newrankvalue =<?php echo $ row ['rank'];?>>< / TD>
< td>< input type =hiddenname =idvalue =<?php echo $ row ['user_id'];?>>< button type =submit class =btn btn-defaultname =btn-updateid =btn-update>更新< /按钮>< / td>
< td>< a href =delete.php?id =<?php echo $ row ['user_id'];?>>删除< / 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']。 ;
$ b $ if($ MySQLi_CON-> query($ sql)=== TRUE){
echoRecord updated successfully;
} else {
echo更新记录时出错:。 $ MySQLi_CON->误差;
}
}
?>

< p>< a href =ny.php>添加新记录< / a>< / p>

< / body>
< / html>


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> 

解决方案

You need to parse the id you wish to modify to the $_POST. Also, you need to use <form action="" method="POST"> in your code.

Didn't tested it, but the following should work:

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

这篇关于用php表单更新一个mysqli表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆