簡體   English   中英

解析錯誤:解析php中的錯誤

[英]Parse error: parse error in php

我有這個php代碼,可以訪問表格用戶和投資組合

但是我一直遇到解析錯誤:第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");
        }
    }
?> 

在第28行:

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

在執行else if之前,您需要使用方括號關閉當前的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");

    }

}

?> 

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");

    }

?>

我這是一個愚蠢的錯誤,在isset之后,id和symbol的聲明應該位於條件的頂部,感謝您的迅速回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM