当我尝试在浏览器中运行此PHP文件时,会收到一条错误消息
当我尝试在浏览器中运行此PHP文件时,会收到错误消息: 分析错误:语法错误,意外“'),第50行应为:(T_PAAMAYIM_NEKUDOTAYIM)” 第54行和第55行也有红色,我无法消除。对于这两行,错误工具提示说 语法错误:应为:( 据我所知,我所有的花括号都在需要的地方。我不知道怎么了。有什么想法吗?代码如下:当我尝试在浏览器中运行此PHP文件时,会收到一条错误消息,php,Php,当我尝试在浏览器中运行此PHP文件时,会收到错误消息: 分析错误:语法错误,意外“'),第50行应为:(T_PAAMAYIM_NEKUDOTAYIM)” 第54行和第55行也有红色,我无法消除。对于这两行,错误工具提示说 语法错误:应为:( 据我所知,我所有的花括号都在需要的地方。我不知道怎么了。有什么想法吗?代码如下: <?php // If a session is NOT already in progress for this // user, start one
<?php
// If a session is NOT already in progress for this
// user, start one:
if(!session_id()){
session_start();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Shopping Cart Manager</title>
<link href="css/minismall.css" rel="stylesheet">
</head>
<body>
<?php
//Check if session cart variable exists and set shortcut:
if (isset($_SESSION['cart'])) {
$cart = $_SESSION['cart'];
}
else { // $_SESSION['cart'] does NOT exist
$cart = Array();
}
include 'catalog.php';
// Check if ‘remove’ form field exists and set shortcut:
if (isset($_POST['remove'])) {
$remove = $_POST['remove'];
}
else { //$_POST['remove'] does NOT exist
$remove = Array();
}
// Initialize $totalPrice, $prodIDStr, and session ‘numItems’
// variables to either 0 or empty string as appropriate:
$totalPrice = 0;
$prodIDStr = "";
$_SESSION[numItems] = 0; // Did I initialize this session
// variable correctly?
//$_SESSION['numItems'] = $numItems;
// Loop through each form field (this page is called
// from catalog.php):
// If a form field’s value (product quantity) is
// a positive number (nonzero), is NOT the submit
// button, AND the remove checkbox for removing this
// product has NOT been checked (do these checks in one
// condition):
while (list($productID, $qty) = each($_POST)){
if (($qty > 0) && (type != submit) && !isset(checkbox)){ // LINE 50
$cat[$productID]['qty'] += $qty; // Update the
// cart's element for this product with
// the product quantity from the form
} //LINE 54
Else if (($qty = 0) || isset(checkbox)){ // If the
// product's quantity is zero or the product's
// remove checkbox is checked
// Remove this product from the cart array:
//unset($_SESSION['cart'][$productID]);
unset($cart['productID']['qty']);
}//END OF Else if
}//END OF while loop
// Connect to your database server and select your database:
require 'dbConnect.php';
// Loop through your cart array (foreach productID's
// quantity in cart):
$_SESSION['numItems'] = $cart; // Update the number of
// items in your cart
// Build a comma-delimited string in $prodIDStr containing the
// product IDs of the products currently in our cart array:
$prodIDStr = implode("productID, qty", $cart);
if($_SESSION[numItems] = 0){ // If cart is empty:
print "<h3>Your shopping cart is empty!</h3>\n";
}
Else { // If cart is not empty:
// Remove trailing comma from $prodIDstr:
// Query the database for all products in our cart
// using the $prodIDStr ordering them
// by category and then by productID:
try {
$prodIDStr = $pdo->query("SELECT * FROM $cart ORDER BY $cat AND $productID");
}
catch (PDOException $e) {
$error = "Error fetching product info for category $cat: " . $e->getMessage();
include 'error.html.php';
exit();
}
// Display beginning of form which calls cart_yourname.php
// when submitted using the POST method:
?>
<form action="cart_speterson86.php" method="post">
<table>
<tr class="header">
<th>Remove</th>
<th>Image</th>
<th>Description</th>
<th>Price - US$</th>
<th>Subtotal</th>
<th>Quantity</th>
<th style="background-color: #fff"> </th>
</tr>
<?php
//
// Step through result set of products in this category
// and display each product in its own table row
//
while ($row = $itemsResult->fetch()) {
// Convert any special HTML characters to their HTML
// entities. Example: & --> &
//
// Also, strip out any HTML tags found in the data
$remove = htmlspecialchars(strip_tags($row['loc']));
$imageLocation = htmlspecialchars(strip_tags($row['loc']));
$desc = htmlspecialchars(strip_tags($row['description']));
$price = htmlspecialchars(strip_tags($row['price']));
$subTotal = htmlspecialchars(strip_tags($row['loc']));
$price = "\$" . number_format($price, 2);
$productID = strip_tags($row['prodid']);
//
// Set $qty to contain what is in our session cart array.
// If your session cart array element for this product is
// empty, set the $qty to its default value of 0.
//
if (isset($_SESSION['cart'][$productID])) {
$qty = $_SESSION['cart'][$productID];
}
else {
$qty = 0;
}
// Build and display next product row in table
print <<<TABROW
<tr>
<td><input type="checkbox" name="$remove[$productID]" value="1"></td>
<td><img src="$imageLocation" alt="groovy product $desc"></td>
<td class="desc">$desc</td>
<td class="price">$price</td>
<td class="price">$subTotal</td>
<td class="qty">
<label for="quantityForProduct$productID">Qty</label>
<input type="text"
name="$productID"
id="quantityForProduct$productID"
value="$qty"
size="3">
</td>
</tr>
TABROW;
} // end while another row in product result set
?>
<tr>
<td colspan="4" id="submitCell">
<input type="submit" name="addCart" value="Add Items to Cart">
</td>
<td><input type="submit" name="checkOut" value="Checkout"></td>
<td><input type="submit" name="updateCart" value="Update Cart"></td>
</tr>
</table>
</form>
<br>
<?php
} // END OF Else cart is not empty
?>
</body>
</html>
购物车经理
变量需要$
,字符串需要“
if($qty>0)&&&(type!=submit)&&&!isset(复选框)){
可能应该是:
if(($qty>0)&&($type!='submit')&&!isset($checkbox)){
与第55行相同(忘记了$
):
Else if(($qty=0)| isset($checkbox)){//if产品的数量
$\u会话[numItems]=0;//我是否初始化了此会话
是否应该使用单引号?哪些行是50、54和55?嗯……我以为stackoverflow具有“显示行号”功能或其他功能。请稍等。我将用注释标记它们。PHP的行为就像未定义的常量被定义为具有常量名称的字符串一样。尽管它会生成注意,它是有效的。(我不建议依赖它。)注意是有原因的。@DaveChen:当然,但这显然不是问题中的错误。呃,伙计,这段代码有很多问题。OP真的需要仔细研究一下,了解到底发生了什么。如果我没有弄错,类似于$id=“e”;If($id=e)回声“hi”;
也可以,但它只将不带引号的文本转换为带引号的文本,而不转换为带变量的文本。第50行的第二个参数:“(type!=submit)”从技术上讲是一个按钮。在表单中,它的名称是“addCart”。所以您告诉我“$addCart!=submit”是否正确地表示尚未单击它?要检查用户是否正在发布,请使用if($\u SERVER['REQUEST\u METHOD']='POST')
。