PHP删除/更新仅影响最后一行MySQL行

时间:2015-05-17 15:23:23

标签: php mysql pdo

无论是使用删除还是更新功能,都只更新/删除最后一行。更新/删除哪个字段并不重要,只传递最后一行。除了没有传递唯一ID之外,我无法找到问题。我是PDO的新手,所以我对调试不太熟悉。任何帮助表示赞赏。 Example of output

<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

?>

3 个答案:

答案 0 :(得分:0)

要调试代码,请在print_r($_POST); exit();后立即尝试使用if(isset($_POST['delete/update'])) {,以便在发布时查看数组中传递的内容。

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

我希望有所帮助!

答案 1 :(得分:0)

$ query = $ pdo-&gt; prepare(&#34;从客户中删除id =:id&#34;

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

答案 2 :(得分:0)

解决了这个问题。将<form>标记移到表格行的开头上方。