此update.php有什么问题?

问题描述 投票:-1回答:1

我正在尝试完成mysqli程序原始应用程序。除了仅更新,一切都正常。这是更新代码。请让我知道怎么了?当我单击编辑并保存我的新更新数据并在index.php上显示表格时,它给了我旧数据而不是更新数据

first update.php

<?php
include('config/connect.php');
$fname = $country ='';
$id = $_GET['id'];
if(isset( $_POST['update'] )){
    $fname = $_POST['name'];
    $country = $_POST['country'];
    $sql = "UPDATE userdata
                SET name='$fname', country='$country' WHERE id='$id' ";
    if( mysqli_query($conn, $sql) ) {
        header('Location: index.php');
    } else {
        echo "error"
    }
}
$sql = "SELECT * FROM userdata WHERE id='$id' ";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_assoc($result);
?>


<!DOCTYPE html>
<html>
<?php include('templates/header.php'); ?>
<section class="addsection">
        <h4 class="text-center">update Your Data</h4>
        <form class="form" action="index.php" method="POST">
         <div class="form-group">
            <label>Name</label>
            <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($fname) ?>">
         </div>   
         <div class="form-group">   
            <label>Country</label>
            <input type="text" name="country" class="form-control" value="<?php echo htmlspecialchars($country) ?>">
         </div>   
        <div class="text-center">
            <input type="submit" name="update" value="EDIT">
        </div>
        </form>
</section>
</html>

然后index.php

<?php
  include('config/connect.php');
    // select all from the table and order them
    $sql = 'SELECT * FROM userdata ORDER BY id ASC';
    // get the data
    $data = mysqli_query($conn, $sql);
    // make the gotten data in array
    $users = mysqli_fetch_all($data, MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<?php include('templates/header.php'); ?>
<div class="container">
<section class="tabledata">
    <table class="table table-hover table-dark">
         <thead>
            <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Country</th>
              <th>Update</th>
            </tr>
         </thead> 
         <tbody> 
            <?php foreach($users as $user): ?>
              <tr>
                <td><?php echo htmlspecialchars($user['id']); ?></td>
                <td><?php echo htmlspecialchars($user['fname']); ?></td>
                <td><?php echo htmlspecialchars($user['country']); ?></td>
                <td>
                  <a href="update.php?id=<?php echo $user['id'] ;?>">Edit</a>
                </td>
              </tr> 
            <?php endforeach; ?>
         </tbody>  
    </table>
</section> 
</div> 
</html>
php mysqli crud edit procedural
1个回答
-1
投票

表格正在提交给index.php,而不是update.php。应该是:

<form class="form" action="update.php?id=<?php echo $id; ?>" method="POST">
© www.soinside.com 2019 - 2024. All rights reserved.