解析错误:解析php中的错误

时间:2015-09-20 09:53:37

标签: php mysql

我有这个访问表用户和投资组合的PHP代码

但是我继续遇到Parse错误:在第28行的/Users/...../Sites/..../sell.php中解析错误

如果有人可以帮助我,那会很有帮助。

<?
    // require common code
    require_once("includes/common.php");
    //check for errors
    $error = false;
    if (isset($_POST["submit"]))
    {
        if(empty($_POST["symbol"]))
        {
            $error = true;
            $message = "Enter a name";
        }
        else if(empty($_POST["shares"]))
        {
            $error = true;
            $message = "Enter the shares";
        }
        // check if user has the stocks and the shares
        $id = $_SESSION["id"];
        $symbol = $_POST["symbol"];
        $sharesQuery = "SELECT shares FROM portfolio WHERE id = $id AND symbol = '$symbol' ";
        else if(($shares = mysql_query($sharesQuery)) == false)
        {
            $error = true;
            $message = "Don't have the stock";
        }
        // else, if everything checks out, delete it and increment the cash
        else 
        {
            $deleteQuery = "DELETE from portfolio WHERE id = $id AND symbol = '$symbol'";
            mysql_query($deleteQuery);
            $incrementQuery = "UPDATE users SET cash = cash + ($shares * lookup($symbol)->price) WHERE id = $id ";
            mysql_query($incrementQuery);

            //set the variables into session  and then redicrect to sell2
            $_SESSION["symbol"] = $_POST["symbol"];
            $_SESSION["shares"] = $_POST["shares"];
            redirect("sell2.php");
        }
    }
?> 

2 个答案:

答案 0 :(得分:1)

第28行:

else if(($shares = mysql_query($sharesQuery)) == false)

如果出现以下情况,您需要一个括号来关闭当前的if语句:

<?

// require common code
require_once("includes/common.php");

//check for errors
$error = false;
// check if user has the stocks and the shares
$id = $_SESSION["id"];
$symbol = $_POST["symbol"];
$sharesQuery = "SELECT shares FROM portfolio WHERE id = $id AND symbol = '$symbol' ";

if (isset($_POST["submit"])){

    if(empty($_POST["symbol"])){
        $error = true;
        $message = "Enter a name";
    }else if(empty($_POST["shares"])){
        $error = true;
        $message = "Enter the shares";
    }else if(($shares = mysql_query($sharesQuery)) == false){
        $error = true;
        $message = "Don't have the stock";
    }else{
        $deleteQuery = "DELETE from portfolio WHERE id = $id AND symbol = '$symbol'";
        mysql_query($deleteQuery);

        $incrementQuery = "UPDATE users SET cash = cash + ($shares * lookup($symbol)->price) WHERE id = $id ";
        mysql_query($incrementQuery);

        //set the variables into session  and then redicrect to sell2
        $_SESSION["symbol"] = $_POST["symbol"];
        $_SESSION["shares"] = $_POST["shares"];
        redirect("sell2.php");

    }

}

?> 

答案 1 :(得分:1)

else从何而来?添加}

} else if(($shares = mysql_query($sharesQuery)) == false)

您的最终代码将是:

<?

    // require common code
    require_once("includes/common.php");

    //check for errors
    $error = false;
    if (isset($_POST["submit"])) {

        if (empty($_POST["symbol"])) {
            $error   = true;
            $message = "Enter a name";
        }

        else if (empty($_POST["shares"])) {
            $error   = true;
            $message = "Enter the shares";
        }

        // check if user has the stocks and the shares
        $id     = $_SESSION["id"];
        $symbol = $_POST["symbol"];

        $sharesQuery = "SELECT shares FROM portfolio WHERE id = $id AND symbol = '$symbol' ";
    } else if (($shares = mysql_query($sharesQuery)) == false) {
        $error   = true;
        $message = "Don't have the stock";
    }

    // else, if everything checks out, delete it and increment the cash

    else {
        $deleteQuery = "DELETE from portfolio WHERE id = $id AND symbol = '$symbol'";
        mysql_query($deleteQuery);

        $incrementQuery = "UPDATE users SET cash = cash + ($shares * lookup($symbol)->price) WHERE id = $id ";
        mysql_query($incrementQuery);

        //set the variables into session  and then redicrect to sell2
        $_SESSION["symbol"] = $_POST["symbol"];
        $_SESSION["shares"] = $_POST["shares"];
        redirect("sell2.php");

    }

?>