我有以下代码,但它似乎对我不起作用

时间:2014-01-13 06:38:30

标签: php mysql

    $dupnamecheck = "SELECT product_name FROM products WHERE product_name = $name";
    $dupnameresult = mysqli_query($link,$dupnamecheck);
    $data = mysqli_fetch_array($dupnameresult, MYSQLI_NUM);
    if($data[0] > 1) {
        echo "Product name is duplicated. <br/>";
    }

    else { 
    $query = ("INSERT INTO products (id, product_name, product_description, category_id, brand_id, views, price) 
    VALUES (null, '".$name."','".$desc."','".$cat."', '".$brand."', '0','".$price."')");

    $status = mysqli_query($link,$query) or die(mysqli_error($link));

    if($status){
    $message = "Successfully Added! Please check on products page or on inventory page in admin panel." ;
}
else {
    $message = "FAILED!";
}   
    } 

我正在尝试使用product_name作为条件来检查产品表中的重复名称。我有这个错误: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

3 个答案:

答案 0 :(得分:0)

您的商品名称可能是一个字符串,因此您需要在变量名称周围加上引号。

SELECT product_name FROM products WHERE product_name = '$name';
                                                   ----^-----^--

答案 1 :(得分:0)

除了第一个SQL语句外,您的代码几乎没有问题。

更正了一个:

 <?php
$dupnamecheck = "SELECT product_name FROM products WHERE product_name = '$name'";
//                                                                      ^     ^ 
        $dupnameresult = mysqli_query($link,$dupnamecheck);
        $data = mysqli_fetch_array($dupnameresult, MYSQLI_NUM);
        if($data[0] > 1) {
            echo "Product name is duplicated. <br/>";
        }

        else { 
        $query = ("INSERT INTO products (id, product_name, product_description, category_id, brand_id, views, price) 
        VALUES (null, '".$name."','".$desc."','".$cat."', '".$brand."', '0','".$price."')");

        $status = mysqli_query($link,$query) or die(mysqli_error($link));

        if($status){
        $message = "Successfully Added! Please check on products page or on inventory page in admin panel." ;
    }
    else {
        $message = "FAILED!";
    }   
        } 
            ?>

答案 2 :(得分:0)

你能试试吗,

'中添加了WHERE product_name = '$name'

已使用 - $dataCount = mysqli_num_rows($dupnameresult);代替$data = mysqli_fetch_array($dupnameresult, MYSQLI_NUM);

从插入查询中删除了(id)列及其null值,假设它是自动增量的主键。

    $dupnamecheck = "SELECT product_name FROM products WHERE product_name = '$name' ";
    $dupnameresult = mysqli_query($link,$dupnamecheck);
    $dataCount = mysqli_num_rows($dupnameresult);
    if($dataCount > 0) {
        echo "Product name is duplicated. <br/>";
    }else {
        $query = "INSERT INTO products (product_name, product_description, category_id, brand_id, views, price)
        VALUES ('".$name."','".$desc."','".$cat."', '".$brand."', '0','".$price."')";

        $status = mysqli_query($link,$query) or die(mysqli_error($link));

        if($status){
            $message = "Successfully Added! Please check on products page or on inventory page in admin panel." ;
        }else {
            $message = "FAILED!";
        }
    }