纠正解析错误:语法错误,意外T_ELSE

时间:2014-03-06 16:27:15

标签: php

我最终在试图找出为什么我的其他声明因意外的T_ELSE而失败。我知道这是一个支架问题,但对于我的生活,我看不到它。有人可以指出我的错误。感谢

<?php


// Connection config
function runSQL($rsql) {
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $dbname   = "sample";
    $connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
    $db = mysql_select_db($dbname);
    $result = mysql_query($rsql) or die (mysql_error());
    return $result;
    mysql_close($connect);
    }
?>
<?php
    // test vars from jquery form
    $status = mysql_real_escape_string($_REQUEST['status']);
    $company = mysql_real_escape_string($_REQUEST['company']);
    $requested = mysql_real_escape_string($_REQUEST['requested']);
    $activity = mysql_real_escape_string($_REQUEST['activity']);
    $address = mysql_real_escape_string($_REQUEST['address2']);
    $service = mysql_real_escape_string($_REQUEST['service']);
    $box = mysql_real_escape_string($_REQUEST['box_rtv']);
    $authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
    $dept = mysql_real_escape_string($_REQUEST['rtv_dept']);

    // Split the box if multiples
    $array = split('[,]', $_REQUEST['box_rtv']);

    // Loop to split if multiple request and check DB for dupe entries
    foreach ($array as $box) {
        $sql = "SELECT item FROM act WHERE item = '$box'";echo $sql;
        $result = runSQL($sql) or die(mysql_error());

        // If there are dupe entries, send message to jquery
        if (mysql_num_rows($result)>0) {
            echo 'Error';
            return;
        }
    } else {

        // If no dupes, then enter values into DB.
        $form = array();
        foreach ($array as $box) {
            $form = array('dept'=>$dept,
            'company'=>$company,
            'address'=>$address,
            'service'=>$service,
            'box'=>$box,
            'destroydate'=>$destroydate,
            'authorised'=>$authorised,
            'submit'=>$submit);

            $sql = "INSERT INTO `temp` (service, activity, department, company, address, user, date, item, new) VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', NOW(), '$box', 1)";
            $result = runSQL($sql) or die(mysql_error());
        }
    }
    ?>

2 个答案:

答案 0 :(得分:3)

您的else目前正在申请foreach,这显然不是您的意图!注意}职位。

答案 1 :(得分:3)

您只需要一个}来关闭if条款

if (mysql_num_rows($result)>0) {
        echo 'Error';
        return;
    } // <-- Remove this
} else {