我正在做一个购物车摘要,尝试自己的教程。但我遇到了一些错误。
声明“警告: mysql_fetch_array()期望参数1是资源,布尔值在C:\xxx\xxx.php的第18行中给出”
我想要显示我已经购买的商品,所以我从购物车数据库中传回这些商品。(使用dreamweaver工具,我只是拖放它)。但是,这些项没有显示,但它给了我这个错误。
我可以知道我的代码出了什么问题吗,把SQL注入放在一边(因为我没有使用它,仍然在尝试基本的)。
<?php require_once('Connections/MyDatabase.php'); ?>
<?php
session_start();
?>
<?php
$cartTotal = 0;
if(isset($_POST['CheckoutBTN']))
{
date_default_timezone_set('Asia/Singapore');
$time=date('D,F j, Y, H:i:s A');
$uname = $_SESSION['MM_Username'];
foreach ($_SESSION["supermarketcart"] as $each_item)
{
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM supermarket WHERE id = '$item_id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$productdes = $row['description'];
$price = number_format($row['price'],2);
$size = $row['packaging'];
$itemqty = $each_item['quantity'];
$pricetotal = $price * $each_item['quantity'];
$cartTotal = number_format($pricetotal + $cartTotal,2);
$checkout="INSERT INTO supermarketcart (itemid, productdes, package, itemprice, qty, username, ddate) VALUES
('$item_id', '$productdes', '$size', '$price','$itemqty','$uname','$time')";
mysql_query($checkout)or die(mysql_error());
}
}
}
?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "login.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_cart = "-1";
if (isset($_SESSION['MM_USERNAME'])) {
$colname_cart = $_SESSION['MM_USERNAME'];
}
mysql_select_db($database_MyDatabase, $MyDatabase);
$query_cart = sprintf("SELECT * FROM supermarketcart WHERE username = %s", GetSQLValueString($colname_cart, "text"));
$cart = mysql_query($query_cart, $MyDatabase) or die(mysql_error());
$row_cart = mysql_fetch_assoc($cart);
$totalRows_cart = mysql_num_rows($cart);
mysql_select_db($database_MyDatabase, $MyDatabase);
$query_user_info = "SELECT * FROM user_data";
$user_info = mysql_query($query_user_info, $MyDatabase) or die(mysql_error());
$row_user_info = mysql_fetch_assoc($user_info);
$totalRows_user_info = mysql_num_rows($user_info);
?>
发布于 2016-09-20 09:16:13
在您的代码片段之后,如果在内部设置了$each_item['item_id']
,则不会检查它。后续查询有时会失败(如果WHERE close为空,SQL将失败)
$sql = mysql_query("SELECT * FROM supermarket WHERE id = '$item_id' LIMIT 1");
所以$sql
将被设置为false
,然后你会得到你的错误。
您可能禁用了警告消息,因此不会从远程mysql或未设置的$each_item['item_id']
获得任何信息。
更改错误报告级别并在之后进行调试:为此,在session_start();
之后添加以下代码
error_reporting(E_ALL);
https://stackoverflow.com/questions/21114308
复制相似问题