PHP Mysql删除查询无法正常工作

时间:2014-10-31 09:00:05

标签: php mysql mysqli

我从我的MYSQL数据库中提取产品列表,并针对每个产品使用删除按钮,以防操作员想要删除产品。

问题是,每当我点击列表中任何产品的删除按钮时,第一个元素都会被删除。

我的代码在下面出了什么问题?

Products页面:

<?php
$link=mysqli_connect("localhost","root","","smartcart");
$prod="select * from products";
$rw=mysqli_query($link,$prod) or die(mysqli_errno()."in query $prod");
$count=1;

while($row=mysqli_fetch_assoc($rw))
{
    echo "<tr>";
    echo "<td>".$count."</td>";
    echo "<td>".$row['prod_id']."</td>";
    echo "<td>".$row['prod_name']."</td>";
    echo "<td>".$row['prod_price']."</td>";
    echo "<td><form action='delete_prod.php' id='delete' method='get'>";
    echo "<input type='hidden' name='prod_id' value='".$row['prod_id']."' />";
    echo "<button type='submit' form = 'delete' class='btn btn-default' name='delete'>Delete</button>";
    echo "</form></td>";
    $count=$count+1;                        
}

mysqli_free_result($rw);
?>

delete_prod.php

<?php
if(isset($_GET['delete']))
{
    include "connection.php";
    $prod_id=$_REQUEST['prod_id'];
        $del="delete from products where prod_id=$prod_id";
    if (mysqli_query($link,$del))
    {
        echo "Successfully deleted";
        unset($_POST['delete']);
    }
    else
    {
        echo "Delete operation Failed";
    }
    header('location:show_db.php');
}
?>

我认为我非常遗漏一些简单的观点,但我无法得到它是什么。

3 个答案:

答案 0 :(得分:3)

很可能是因为您设置了id="delete"。通常id属性值不重复。

echo "<td><form action='delete_prod.php' id='delete' method='get'>";
echo "<button type='submit' form = 'delete' class='btn btn-default' name='delete'>Delete</button>";

提交按钮获取第一个ID,从而获得第一个隐藏的输入。

或者,你可以像这样设计你的按钮并作为你的标记:

无需打印每张表格!只需用表格包裹它:

echo "<form action='delete_prod.php' id='delete' method='get'>";

echo '<table>';
while($row = mysqli_fetch_assoc($result)) {
    $prod_id = $row['prod_id'];
    echo "<tr>";
        echo "<td>".$count."</td>";
        echo "<td>".$row['prod_id']."</td>";
        echo "<td>".$row['prod_name']."</td>";
        echo "<td>".$row['prod_price']."</td>";
        echo "<td>";
        // each id is assigned to each button, so that when its submitted you get the designated id, the one that you clicked
        echo "<button type='submit' value='$prod_id' class='btn btn-default' name='delete'>Delete</button>";
        echo "</td>";
    echo '</tr>';
}

echo '</table>';
echo '</form>';

然后在PHP处理中:

if(isset($_GET['delete'])) // as usual
{
    include "connection.php";
    $prod_id = $_GET['delete']; // get the id
    // USE PREPARED STATEMENTS!!!
    $del="DELETE FROM products WHERE prod_id = ?";
    $delete = $link->prepare($del);
    $delete->bind_param('i', $prod_id);
    $delete->execute();
    // don't echo anything else, because you're going to use header
    if($delete->affected_rows > 0) {
        header('location:show_db.php');
    } else {
        echo 'Sorry delete did not push thru!';
    }
}

答案 1 :(得分:0)

检查prod_id是否在表格中正确自动递增。另一件事是当你的表单在循环中时,所有表单的id都将被复制。因此,每次提交第一个表单时,这就是为什么只有第一个产品从您的记录中删除。

答案 2 :(得分:0)

$link=mysqli_connect("localhost","root","","smartcart");
    $prod="select * from products";
    $rw=mysqli_query($link,$prod) or die(mysqli_errno()."in query $prod");
    $count=1;
    while($row=mysqli_fetch_assoc($rw))
    {
        echo "<tr>";
        echo "<td>".$count."</td>";
        echo "<td>".$row['prod_id']."</td>";
        echo "<td>".$row['prod_name']."</td>";
        echo "<td>".$row['prod_price']."</td>";
        echo "<td><form action='delete_prod.php'  method='get'>";
        echo "<input type='hidden' name='prod_id' value='".$row['prod_id']."' />";
        echo "<input type='submit' value='Delete' class='btn btn-default' name='delete'/>";
        echo "</form></td>";
        $count=$count+1;                        
    }

删除delete_prod.php中的操作代码

     if(isset($_GET['delete']))
{
    include "connection.php";
    $prod_id=$_REQUEST['prod_id'];
        $del="delete from products where prod_id=$prod_id";
    if (mysqli_query($link,$del))
    {
        echo "Successfully deleted";
        unset($_GET['delete']);
    }
    else
    {
        echo "Delete operation Failed";
    }
    header('location:show_db.php');
}

试试这个......