gpt4 book ai didi

php - 奇怪的 "unexpected end of file"错误

转载 作者:行者123 更新时间:2023-11-29 04:41:24 25 4
gpt4 key购买 nike

最近我完成了一个小的 php 项目,在我的 2 台 mac 上使用 xampp 显示我的作品时一切正常,但是在使用 xampp 的 Windows 机器上测试时我不断收到此错误:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\IT2B\cart.php on line 161

我已经知道这个错误意味着我的代码中某处有一个未闭合的括号,但是经过几个小时的搜索我仍然无法解决这个错误信息。我数了每个左括号,打开和关闭的数字相同,表示所有括号都已关闭。

我什至尝试使用 error_reporting(0); 函数来抑制所有错误(是的,我知道这不是一个好主意),看看它是否能让我的网站显示在Windows机器,但仍然显示错误!这非常令人沮丧,所以我想知道如何解决这个问题。

这是我的 cart.php 代码:

<?php

session_start();

$page = 'index.php';

mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('cart') or die(mysql_error());

if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
$_SESSION['cart_' . (int)$_GET['add']] +='1';
header('Location: order.php');

}
}
header('Location: order.php');

}

if (isset($_GET['remove'])) {
$_SESSION['cart_'.(int)$_GET['remove']]--;
header ('Location: order.php');

}

if (isset($_GET['delete'])) {
$_SESSION['cart_' . (int)$_GET['delete']]='0';
header ('Location: order.php');
}



function products() {
$get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id ASC');
if (mysql_num_rows($get) == 0) {
echo "There are no products to display.";
}
else {
echo "<center>\n";
echo " <table class='menufinal' border=0 width=75%>\n";
echo " <tr>\n";
echo " <th>View</th>\n";
echo " <th>Dish</th>\n";
echo " <th>Description</th>\n";
echo " <th>Item Price</th>\n";
echo " </tr>\n";
while ($get_row = mysql_fetch_assoc($get)) {

?>
<tr>
<td><img src="template.png" height="110" width="110"/> </td>
<td> <?echo '<p><strong>'.$get_row['name'] . '</strong>'?> </td>
<td> <?echo $get_row['description']?> </td>
<td><strong> <?echo '<br>&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'"><button>Add</button></a></p>';?> </strong></td>
</tr>
<?
}
echo "</table>\n";
echo "</center>\n";
}
}

function cart() {
$total=0;

$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';

foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$total += $sub;
$output .= '<tr>';
$output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none"><strong>- </strong></a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
$output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
$_SESSION['total'] = $total;
}
}

$output .= '</table>';

if (empty($total)){
session_destroy();
header ('Location: index.php');
exit;
}

$output .= '<br><br><br><br><div id="finalPrice">Total: &pound ' . number_format($total, 2) . '<br></div>';
$output .= '<br><br><br><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="110"> <a href="checkout.php"><img src="checkout.png" width="240" height="151"></a>';

echo $output;
}

function cartconfirm() {
$total=0;

$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';

foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$total += $sub;
$output .= '<tr>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td>' .$value. '</td>';
$output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
$_SESSION['total'] = $total;
}
}

$output .= '</table>';

if (empty($total)){
session_destroy();
header ('Location: index.php');
exit;
}

$output .= '<br><br><br><br><div id="finalPrice">Total: &pound ' . number_format($total, 2) . '<br></div>';

echo $output;
}

?>

最佳答案

如上所述,您的 PHP 解析器似乎没有正确读取短标记。对评论者抱歉,我不是想窃取答案,我只是将代码粘贴到 NetBeans 中,然后在其中进行更正。

附言。 Netbeans 是一个很棒的调试工具,可以轻松帮助您解决这个问题!它是免费下载的,在您处理 PHP 项目时非常有用。

<? 更改第 59 行至 <?php , 并且文件应该继续读到最后。

关于php - 奇怪的 "unexpected end of file"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27925349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com