查询为空PHP错误 [英] Query was empty PHP error
问题描述
我正在尝试使用MySQL构建购物车.运行此代码时,我不断收到此错误查询为空".请帮助我尝试了几件事,例如将变量放在字符串中而不是将其串联.
I am trying to build a cart using MySQL. I keep getting this error 'Query was empty' when I run this code. Please help I've tried several things such as putting the variables inside the string instead of concatenating it.
<?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');
?>
推荐答案
您尝试用此行实现什么?
What do you try to achieve with this line?
$result = mysql_query($query);
只需删除它,然后将上面的行更改为
Just delete it and change the line above to
$result = mysql_query('insert into cart values('.$user.','.$itemNum.','.$name.', '.$image.','.$quantity.', '.$price.')');
顺便说一句.您忘记了查询内部的"
(引号),这会导致发生SQL错误,导致出现$query = false
(请参见
Btw. you are forgetting the "
(quotes) inside the query which causes an sql error to occur, leading to $query = false
(see manual). $query
(false
) then gets converted to string, resulting in ''
(an empty string) which you pass to mysql_query
and that generates an "Query was empty"-Message, as it should because you tried to send an empty query.
这篇关于查询为空PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!