0
我正在做一個總結購物車,試用我自己的教程。 但我遇到了一些錯誤。嘗試從數據庫中檢索數據時發生mysql_fetch_array()錯誤
有人說, 「警告:mysql_fetch_array()預計參數1是資源,布爾在C中給出:\ XXX \ XXX \ XXX \ XXX \ 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);
?>
可以爲select查詢添加mysql_error(),很可能查詢在循環中的某處失敗 –
你的意思是這樣嗎? (mysql_query()),或者死(mysql_error()) – user3156220
是的,就像你爲插入查詢所做的一樣 –