【问题标题】:PHP MySQL delete rowPHP MySQL删除行
【发布时间】:2014-01-03 10:24:30
【问题描述】:

我到处搜索,找不到答案,我相信我有正确的代码,但可能有错字。

这里出了什么问题?

我有将产品 ID 正确发布到 url 的链接,如下所示:

userAccount.php:

while($columnDelete = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        echo "<section class='product'>
                <a href='extras/deleteProcess.php?productId=".$columnDelete['productId']."' class='deleteProduct' style='color:#990000;font-family:arial;font-weight:bold;font-size:12pt;background:transparent;'>Delete?</a>
                <section class='productImg'>
                    <a target='_self' href='fullProductInfo.php?productId=".$columnDelete['productId']."'>
                        <img src='http://www.littlepenguindesigns.co.uk/pages/CMX/images/products/".$columnDelete['productImg']."' alt='".$columnDelete['productName']."' border='0' width='230' height='200' border='0' />
                    </a>
                </section>
                <section class='productName'><a target='_self' href='fullProductInfo.php?productId=".$columnDelete['productId']."'>".$columnDelete['productName']."</a></section>
                <section class='productPrice'>&pound;".$columnDelete['price']."</section></section>";
    }

$columnDelete['productId']; 将正确的 ID 发布到 url 和 deleteProcess.php 页面,我可以在 URL 中看到 productId,并且我也将其回显到页面上进行检查,它确实显示:

deleteProcess.php:

$productId = $_GET['productId'];
$con = mysqli_connect("BLAH","BLAH","BLAH","BLAH") or die('Server connection not possible.');
$sql = ("DELETE FROM `product` WHERE `product`.`productId`= $productId");
mysqli_query($con, $sql);

echo "Deleted product ID: $productId successfully.<br /><br /><br /><br /><br /><br /> <a href='../userAccount.php#deletion'>Go back to user account and delete another.</a>";

我不明白发生了什么,产品被调用到deleteProcess.php 并进入页面但没有删除,它也没有显示错误。由于我对 php 和 mysql 不熟悉,我认为我最好研究一下,因为我想不出任何答案我想问,所以任何人都可以告诉我我做错了什么或指出我正确的方向。

【问题讨论】:

  • 检查mysql是否连接
  • echo 查询然后直接运行到 phpmyadmin 看看是什么错误

标签: php mysql delete-row corresponding-records


【解决方案1】:
$sql = ("DELETE FROM `product` WHERE `product`.`productId`= $productId");
mysqli_query($con,$sql);

$sql = "DELETE FROM `product` WHERE `product`.`productId`= $productId";
mysqli_query($con,$sql) OR DIE(mysqli_error($con)); //useful for debugging

警告!此代码易受 SQL 注入攻击。 通过清理所有用户输入来修复 sql 注入。

$productId = mysql_real_escape_string($_GET['productId']); // use mysql_real_escape_string on $_GET
$con = mysqli_connect("BLAH","BLAH","BLAH","BLAH") or die('Server connection not possible.');
$sql = "DELETE FROM `product` WHERE `product`.`productId`= '$productId'"; //add single quotes around variable $productid to seperate string from query
mysqli_query($con, $sql);

【讨论】:

    【解决方案2】:

    检查查询执行是否返回成功

    $productId = $_GET['productId'];
    $con = mysqli_connect("BLAH","BLAH","BLAH","BLAH") or die('Server connection not possible.');
    $sql = ("DELETE FROM `product` WHERE `product`.`productId`= $productId");
    
    $result = mysqli_query($con, $sql);
    if(!$result)
       die("Query failed".mysql_error());
    
    echo "Deleted product ID: $productId successfully.<br /><br /><br /><br /><br /><br /> <a href='../userAccount.php#deletion'>Go back to user account and delete another.</a>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多