PHP delete/update 只影响最后 MySQL 行

PHP delete/update only affects last MySQL row

无论使用删除还是更新功能,只有最后一行是updated/deleted。不管我update/delete是什么字段,只传最后一行。除了未传递唯一 ID 之外,我找不到其他问题。我是 PDO 的新手,所以我不太熟悉调试。任何帮助表示赞赏。

<form action="" id="form" method="post">
<?php
function UserForm($customers = array())
    { 
        ob_start(); ?>
        <?php
            $id = $customers['id']; 
        ?>
            <tr>
                <td><input type="text" name="name" value="<?php echo $customers['name']; ?>"></td>
                <td><input type="text" id="email" name="email" value="<?php echo $customers['email']; ?>"></td>
                <td><input type="text" id="phone" name="phone" value="<?php echo $customers['phone']; ?>"></td>
                <td><input type="text" id="address" name="address" value="<?php echo $customers['address']; ?>"></td>
                <td><input type="text" id="proudct" name="product" value="<?php echo $customers['product']; ?>"></td>
                <td><input type="text" id="firmware" name="firmware" value="<?php echo $customers['firmware']; ?>"></td>
                <td><input type="text" id="datepicker" class="datepicker" name="purchase_date" value="<?php echo $customers['purchase_date']; ?>"></td>
                <td align="center">
                    <input type="hidden" name="id" value="<?php echo $id; ?>">
                    <input type="submit" value="<?php echo $id; ?>" name="delete" value="X" onclick="return confirm('WARNING! \n\nAre you sure you want to DELETE?')" >
                </td>
            </tr>
            <tr>
                <td colspan="8">
                <input type="hidden" name="id_update" value="<?php echo $id; ?>" />
                <input type="submit" name="update" value="Update <?php echo $id; ?>" />
                </td>
            </tr>

        <?php
        $data   =   ob_get_contents();
        ob_end_clean();
        return $data;
    } ?>



<?php
$pdo    =   new PDO("mysql:host=localhost;dbname=project", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//$query  =   $pdo->prepare("SELECT * FROM customers ORDER BY purchase_date ASC");
if (isset($_POST['desc'])){
$sort = "desc";
$query  =   $pdo->prepare("SELECT * FROM customers ORDER BY purchase_date DESC");
}
else {
$sort = "asc";
$query  =   $pdo->prepare("SELECT * FROM customers ORDER BY purchase_date ASC");
} 
$query->execute(); 
?>
<table class="table table-striped table-bordered table-responsive">
<thead>
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Address</th>
        <th>Product</th>
        <th>Firmware Version</th>
        <th align="center">
        Purchase Date
        <?php 
        if ($sort == "asc") {
        echo '<input type="hidden" value="Desc" name="desc" id="sort">'; 
        echo '<a name="desc" href="javascript: submitform()">Desc</a>';
        }
        else {
        echo '<input type="hidden" value="Asc" name="asc" id="sort">'; 
        echo '<a name="asc" href="javascript: submitform()">Asc</a>';
        }
        ?>
        </th>
        <th>Delete</th>
    </tr>
</thead>

<?php
while($customers = $query->fetch(PDO::FETCH_ASSOC)){
echo UserForm($customers);
} //end of while

// Delete customer
if(isset($_POST['delete'])) {
try{
        $id     =   $_POST['id'];
        $query  =   $pdo->prepare("delete from customers where id = :id");
        $query->bindParam(':id', $id);
        $query->execute(array(':id' => $id));
        echo "Customer successfully deleted." . $_POST['id'];
        echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
    }catch(PDOException $e){
        echo "Failed to delete the MySQL database table ... :".$e->getMessage();
    } //end of try
} //end of isset delete

// Edit customer
if(isset($_POST['update'])) {
try {
        $name = $_POST['name'];
        $id = $_POST['id']; 
        $email = $_POST['email'];
        $phone =  $_POST['phone'];
        $address = $_POST['address'];
        $product = $_POST['product'];
        $firmware = $_POST['firmware'];
        $purchase_date = $_POST['purchase_date'];  
        $query = $pdo->prepare("UPDATE customers SET name = '$name', email = '$email', phone = '$phone', address = '$address', product = '$product', firmware = '$firmware', purchase_date = '$purchase_date' where id = '$id'");
        $query -> execute( array(
        ':name' => $name,
        ':email' => $email,
        ':phone' => $phone,
        ':address' => $address,
        ':product' => $product,
        ':firmware' => $firmware,
        ':purchase_date' => $purchase_date
        ));
        echo "Customer succesfully updated" . $id;
        echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
        }catch(PDOException $e){
        echo "Error! Failed to update customers :".$e->getMessage();
}//end of try
} //end of isset update

?>

要调试您的代码,请尝试在 if(isset($_POST['delete/update'])) { 之后立即使用 print_r($_POST); exit(); 以查看在您 post.

时数组中传递的内容

我自己有点菜鸟,但我怀疑这里的问题可能是您没有定义表单的开始和结束位置,所以您提交了整个 table。尝试为您的每条记录添加一个表单,表单名称与您的客户 ID 相同。 <form name="formname<?php echo $id; ?>"> ...您的输入字段和提交按钮... </form>,然后当您提交时,您只会提交该特定表单及其包含的数据。

希望对您有所帮助!

$query = $pdo->准备("delete from customers where id = :id"

我打赌这个 ID 是独一无二的..

问题已解决。将 <form> 标记移到 table 行开头上方。