编程语言
首页 > 编程语言> > 查询为空PHP错误

查询为空PHP错误

作者:互联网

我正在尝试使用MySQL建立购物车.运行此代码时,我不断收到此错误“查询为空”.请帮助我尝试了几件事,例如将变量放在字符串中而不是将其串联.

<?php ob_start(); ?><?php require_once("../include/membersite_config.php"); ?>
<?php 
    require('../products_reloaded/config.php');
    session_start();

            $user = $_REQUEST['user'];
            $user = mysql_real_escape_string($user);
            $itemNum = $_REQUEST['itemNum'];
            $itemNum = mysql_real_escape_string($itemNum);
            $quantity = $_POST['quantity'];
            $quantity = intval($quantity);
            $CheckForExistence = mysql_query("select * from cart where user = '$user' and p_number = '$itemNum'" );
            $alreadyExistsChecker = mysql_num_rows($CheckForExistence);
            if($alreadyExistsChecker >= 1)
            {
                $quantity +=1;
                echo "this is equal to $alreadyExistsChecker";
            }

            if($alreadyExistsChecker == 0)
            {
                $getQuery = mysql_query("select * from product where p_number = '$itemNum'");


                while($row = mysql_fetch_array($getQuery))
                {
                    $name = $row['p_name'];
                    $image = $row['p_url'];
                    $price = $row['p_price'];
                }
                $name = mysql_real_escape_string($name);
                $image = mysql_real_escape_string($image);
                $price = intval($price);
                $query = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')'); 
                $result = mysql_query($query);
                if (!$result) {
                    print "An error occured: " . mysql_error() . "\n";
                }

            }

            header('http://www.definitionxjm.com/shopping/viewCart.php');



?>

解决方法:

您想用这条线做什么?

$result = mysql_query($query);

只需将其删除并将上面的行更改为

$result = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')'); 

[编辑]

顺便说一句.您忘记了查询中的“(引号),这会导致发生SQL错误,导致$query = false(请参阅manual).$query(false)随后将转换为字符串,从而导致”(空字符串)传递给mysql_query并生成“查询为空”消息,这是因为您试图发送空查询而引起的.

标签:php,mysql,e-commerce
来源: https://codeday.me/bug/20191013/1908145.html