选择不起作用后删除

时间:2015-05-24 14:41:57

标签: mysql database wordpress select phpmyadmin

在这个网站上我正在开发一个真正的产品代码验证系统,所以当用户在表单中插入代码时,这将向他们展示他们的产品是否是真品,但是用户只能在他们的代码之后验证他们的产品一次从数据库中删除。我已经成功创建了"搜索"部分。当我试图删除相同的记录时,它无法正常工作。我想在选择后立即删除。

请查看此代码并建议我如何删除该记录。 我不是专业的初学者。

<?php
$produt_code = $_GET["product_code"];

$servername = "localhost";
$username   = "#";
$password   = "#";
$dbname     = "#";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql    = "SELECT product_code FROM product_verify WHERE product_code=" . $_GET["product_code"];
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<font color="green"><h3>Congratulations!! You Have Genuine Product</h3></font>';

    }
} else {
    echo '<font color="red"><h3>Sorry!! You Dont Have A Genuine Product.</h3></font><br><font color="red"><h3>NOTE:</h3></font><font color="red"><h3>1.Please check for spelling mistakes and try again.</h3></font><font color="red"><h3>2.May be this product code is already verified.</h3></font>';

}
?>

我在select语句

之后使用此代码
$sql2 = "DELETE FROM product_code WHERE product_code=" . $_GET["product_code"];
    $result2 = mysqli_query($conn, $sql2);

但它没有删除该记录我试图在while语句之后发布相同的代码但没有用 试过这个

mysqli_query = "SELECT city_name,product_code FROM code_verify WHERE product_code=" . $_GET["product_code"];
    $query = mysql_affected_rows();
       if($query > 0)
       {
       $sql = "DELETE FROM product_code WHERE product_code=" . $_GET["product_code"];

但不是这个有效 还在同一个文件中尝试了另一个用于删除的php

2 个答案:

答案 0 :(得分:0)

首先不要在PHP标记中使用“query”。“variable”,所以删除它并替换此

$sql = "SELECT product_code FROM product_verify WHERE product_code = $_GET["product_code"]";

第二件事从echo的两边删除单引号并使用“”双引号外侧并在双引号内使用单引号。

回声“

恭喜!!你有正版产品

”;

答案 1 :(得分:0)

我会将删除查询放在while:

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<font color="green"><h3>Congratulations!! You Have Genuine Product</h3></font>';
        $sql2 = "DELETE FROM product_code WHERE product_code=".$_GET['product_code'];
        $result2 = mysqli_query($conn, $sql2);
    }

这样,只有在您确定用户将获得反馈时才会执行查询。