PHP Mysql删除查询无法正常工作 [英] PHP Mysql delete Query not working properly

查看:53
本文介绍了PHP Mysql删除查询无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am pulling a list of products from my MYSQL database and using a delete button against each product in case the operator wants to delete the product.

问题在于,每当我点击列表中任何产品上的删除按钮时,第一个元素就会被删除.

The problem is that every time I hit the delete button on any product in the list, the first element gets deleted.

下面的代码有什么问题?

What's wrong with my code below ?

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');
}
?>

我认为我非常想念一些简单的要点,但无法理解它是什么.

I think I am terribly missing some simple point, but am unable to get what is it.

推荐答案

最有可能是因为您设置了id="delete".通常,id属性值不会重复.

Most likely because you setup the id="delete". Usually id attribute values are not duplicated.

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,从而获得第一个隐藏的输入.

The submit button gets the first ID and thus getting the first hidden input.

或者,您可以像这样设计按钮并用作标记:

Alternatively, you could devise your button like this and serve as your marker:

无需打印每个表格!只需将它包装在桌子上即可:

No need to print each form!. Just wrap it with the table:

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!';
    }
}

这篇关于PHP Mysql删除查询无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆