php - 奇怪"意外的文件结尾"错误

时间:2015-01-13 15:14:28

标签: php mysql syntax

最近我完成了一个小型的php项目,一切都在我的2台mac上完美运行,使用xampp显示我的工作,但是当使用xampp在Windows机器上测试时,我一直收到这个错误:

  

解析错误:语法错误,第161行的C:\ xampp \ htdocs \ IT2B \ cart.php中的文件意外结束

我已经知道这个错误意味着我的代码中有一个未封闭的括号,但经过几个小时的搜索后,我仍然无法解决此错误消息。我已计算每个左括号,并且打开的数字与关闭时相同,表示所有括号都已关闭。

我甚至尝试使用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;
}

?>

2 个答案:

答案 0 :(得分:3)

如上所述,您的PHP解析器似乎没有正确读取短标签。对于评论者,我没有试图窃取答案,我只是将代码粘贴到NetBeans中,然后在那里进行了更正。

PS。 Netbeans是一个很好的调试工具,可以很容易地帮助你!它是免费下载的,当您使用PHP项目时非常棒。

将第59行从<?更改为<?php,文件应继续向下读到最后。

答案 1 :(得分:1)

只需将<?更改为<?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>
    <?php
    } 
    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>';

return $output;
}

?>

说明: 从此链接http://uk.php.net/ini.core

获取解释

希望这有助于你