使用PHP&删除数据库中的数据JavaScript的

时间:2015-11-08 18:29:43

标签: javascript php mysql database window.location

我正在处理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>

4 个答案:

答案 0 :(得分:1)

你忘记了'_GET [确实]做了''周围的事情:

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}");

答案 1 :(得分:1)

问题在于您的javascript,product_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");
}

出于调试目的,请尝试将其替换为您的代码,并告诉我们您收到的错误消息。

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

另外还尝试在没有单引号的情况下运行查询,我假设product_id是一个整数。

所以PageVirtualServer

答案 2 :(得分:0)

您应该尝试: 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++;
    }

答案 3 :(得分:0)

  <?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>