PHP 更新操作不起作用


php update operation not working

>我正在学习 php 中的 CRUD mysqli 插入和删除正在工作,但是当我尝试更新任何记录时,新值记录入到新行中,而不是将旧记录值替换为新值。 我有以下代码用于更新代码,请建议缺少什么

主页

<?php
include 'db.php';
            if(isset($_GET['del_id']))
            {
                $del="DELETE FROM student WHERE id = '$_GET[del_id]'";
                $run=mysqli_query($con,$del);
            }   
?>
<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>  
    <div class="container">
        <table class="table">
            <th>Id</th>
            <th>Name</th>
            <th>Password</th>
            <th>Address</th>
            <th>View</th>
            <th>Update</th>
            <th>Delete</th>
<?php
include 'db.php';

   $show="select * from student";
   $run=mysqli_query($con,$show);   
   while($row=mysqli_fetch_array($run))
   {
        echo ' 
                <tr>
                        <td>'.$row[0].'</td>
                        <td>'.$row[1].'</td>
                        <td>'.$row[2].'</td>
                        <td>'.$row[3].'</td>
                        <td><a class="btn btn-success btn-xs" href="view.php?user_id='.$row[0].'">View</a></td>
                        <td><a class="btn btn-info btn-xs" href="home.php?update_id='.$row[0].'">Update</a></td>
                        <td><a class="btn btn-danger btn-xs "href="test.php?del_id='.$row[0].'">Delete</a></td>
                </tr>
        ';
   }
?>
        </table>
    </body>
    </html>`

更新页面

  <?php
include 'db.php';

                if(isset($_GET['update_id']))
            {
                $up="SELECT FROM student WHERE id = '$_GET[update_id]'";
                $run=mysqli_query($con,$up);
                 while($row=mysqli_fetch_array($run))
                    {
                        $name=$row['txtname'];
                        $pass=$row['txtpass'];
                        $add=$row['txtadd'];
                    }
            }
            else
            {
                        $name='';
                        $pass='';
                        $add='';
            }
?>
<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<div class="container">
  <form class="form-horizontal" role="form" action="process.php" method="POST">
    <div class="form-group">
      <label class="control-label col-sm-2" for="name">name:</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" id="name" name="txtname"  value="<?php echo $name;?>" placeholder="Enter Name">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2" for="pwd">Password:</label>
      <div class="col-sm-10">          
        <input type="password" class="form-control" id="pwd" name="txtpass" value="<?php echo $pass;?>" placeholder="Enter password">
      </div>
    </div>
     <div class="form-group">
      <label class="control-label col-sm-2" for="email">Address:</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" id="address" name="txtadd" value="<?php echo $add;?>" placeholder="Enter Address ">
      </div>
    </div>
    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
          <label><input type="checkbox"> Remember me</label>
        </div>
      </div>
    </div>
    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-default" name="submit_form">Submit</button>
      </div>
    </div>
  </form>
</div>
    </body>
    </html>

当你从数据库中获取行时,试试这个。

$uid = '$_GET[update_id]';
$up="SELECT FROM student WHERE id = '$uid' LIMIT 1";

此外,您的代码容易受到 sql 注入的影响。寻找防止这种情况的方法。

如前所述,您的代码没有UPDATE查询,换句话说,您的"更新页面"代码根本不正确,不会更新任何内容。

将其替换为以下内容:

<?php
include 'db.php';
$id = $_GET['update_id'];
if(isset($id))
{
    $name = $POST['txtname'];
    $pass = $POST['txtpass'];
    $add = $POST['txtadd'];
    $up="UPDATE student SET 'txtname'=$name, 'txtpass'=$pass, 'add'=$add WHERE 'id'=$id";
    mysqli_query($con,$up);
}
?>

注意:您的代码容易受到 SQL 注入攻击,一旦更新代码问题得到解决,您应该对其进行改进。