繁体   English   中英

使用PHP和JavaScript从数据库中删除数据

[英]Delete data from database using PHP & JavaScript

我正在处理PHP文件。 我正在尝试制作一个表,该表显示数据库中的产品列表。 还有一个用于删除任何产品的按钮。 我已使用JavaScript从数据库中删除产品。 我已经编写了代码,找不到任何错误。 当我单击删除按钮时,它向我显示确认框,但不删除产品。 这是代码:

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);

error_reporting(E_ALL^E_NOTICE);
session_start();


    $sql = mysql_query("select * from products");


if($_GET['did']){
    mysql_query("delete from products where product_id='$_GET[did]'");
    header("Location: product.php");
}

?>
<table border="1px" style="width:100%">
    <tr>
    <th>Serial No</th>
    <th>Product Name</th>       
    <th>Product Type</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Delete Product</th>
  </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>

    <?php
    $i=1;
    while ($u = mysql_fetch_array($sql)) {
        ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $u['product_name'];?></td>
            <td><?php echo $u['product_type'];?></td>
            <td><?php echo $u['quantity'];?></td>
            <td><?php echo $u['price'];?></td>
            <td><?php echo "<a href=\"javascript:delproduct(id=$u[product_id])\">Delete</a>";?></td>

        </tr>
        <?php
    $i++;
    }
    ?>

    <script>
    function delproduct(id){
        var msg = confirm("Are you sure you want to delete this product?");

    if (msg) {
        window.location = "product.php?did="+product_id;
    }
    }
    </script>
</table>

您忘记了$ _GET [did]中的“”操作:

mysql_query("delete from products where product_id='{$_GET['did']}'");

另外,正如@ chris85所指出的那样,直接使用$ _GET或$ _POST不是一个好主意,记住在查询中使用它们之前要清理掉这些值。

$did = filter_input(INPUT_GET, 'did', FILTER_SANITIZE_NUMBER_INT);
mysql_query("delete from products where product_id={$did}");

问题出在您的JavaScript中,product_id不存在

   if (msg) {
        window.location = "product.php?did="+id;
    }

为了进行调试,请尝试用您的代码替换它,并让我们知道您收到的错误消息。

if($_GET['did']){
    mysql_query("delete from products where product_id='".$_GET['did']."'");
    echo "delete from products where product_id='".$_GET['did']."'";
    echo mysql_errno() . ": " . mysql_error() ;    
    die();
    //header("Location: product.php");
}

另外还要尝试在不使用单引号的情况下运行查询,我假设product_id是整数。

因此, mysql_query("delete from products where product_id=".$_GET['did']);

您应该尝试: delproduct($ u [product_id])而不是delproduct(id = $ u [product_id])

mysql_query("delete from products where product_id='".$_GET['did']."'");

while ($u = mysql_fetch_array($sql)) {
        ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $u['product_name'];?></td>
            <td><?php echo $u['product_type'];?></td>
            <td><?php echo $u['quantity'];?></td>
            <td><?php echo $u['price'];?></td>
            <td><?php echo "<a href=\"javascript:delproduct($u[product_id])\">Delete</a>";?></td>

        </tr>
        <?php
    $i++;
    }
  <?php

 $con=mysql_connect("localhost","root","");
 mysql_select_db("grocery_shop",$con);

 error_reporting(E_ALL^E_NOTICE);
session_start();


$sql = mysql_query("select * from products");


if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}

?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>       
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
 </tr>
   <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>

<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
    ?>
    <tr>
        <td><?php echo $i; ?></td>
        <td><?php echo $u['product_name'];?></td>
        <td><?php echo $u['product_type'];?></td>
        <td><?php echo $u['quantity'];?></td>
        <td><?php echo $u['price'];?></td>
        <td><?php echo "<a href=\"javascript:delproduct(".$u[product_id].")\">Delete</a>";?></td>

    </tr>
    <?php
     $i++;
     }
    ?>

<script>
function delproduct(id){
     var product_id = id;
    var msg = confirm("Are you sure you want to delete this product?");

   if (msg) {
      window.location = "product.php?did="+product_id;
  }
  }
   </script>
</table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM