0

I have this php code that accesses the tables users and portfolio

However I keep running into Parse error: parse error in /Users/...../Sites/..../sell.php on line 28

If someone could help me out, it'd be of great help.

<?
    // 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
  • Use code snippets only for HTML / CSS / JavaScript. Commented Sep 20, 2015 at 9:55
  • Highlight the line in which you are getting errors.. Commented Sep 20, 2015 at 9:55

3 Answers 3

1

From where is this else coming in? Add a }:

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

Your final code will be:

<?

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

    }

?>
Sign up to request clarification or add additional context in comments.

Comments

1

On line 28:

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

You need a bracket to close the current if statement before doing an else 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");

    }

}

?> 

Comments

0

It was silly mistake on my part, the declarations for id and symbol should have been at the top of the condition, right after isset, Thanks for the speedy answers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.