我正在做一个摘要购物车,我自己试一下这个教程。 但我遇到了一些错误。
有人说“警告:mysql_fetch_array()期望参数1是资源,布尔值在第18行的C:\ xxx \ xxx \ xxx \ xxx \ xxx \ xxx.php中给出”
我想显示我购买的商品,所以我从购物车数据库中传回商品。 (使用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);
?>
答案 0 :(得分:0)
根据您的代码段,如果设置了内容,则不会检查$each_item['item_id']
。后续查询有时会失败(如果WHERE关闭为空,则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);