Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Please help me to fix PHP bugs

    My script has a two bugs, which I've been unable to fix. So, I need your help. Here is the file:
    PHP Code:
    <?php
        
    // configuration
        
    require("../includes/config.php");
        
    // check if form was submitted
        
    if ($_SERVER["REQUEST_METHOD"] == "POST")
        {
        
    // type of transaction - for tracking history
        
    $transaction 'SELL';
        
    // validate submission
        
    if (empty($_POST["symbol"]))
        {
        
    apologize("Select a stock to sell.");
        }
        else if (!empty(
    $_POST["symbol"]))
        {
        if (
    $stock lookup($_POST["symbol"]))
        {
        
    // select database
        
    $shares query("SELECT shares FROM portfolios WHERE id = ?"$_SESSION["id"], $_POST["symbol"]);
        
    $value $stock["price"] * $shares[0]["shares"];
        
    // delete stock's data from database table
        
    query("DELETE FROM portfolios WHERE id = ? AND symbol = ?"$_SESSION["id"], $_POST["symbol"]);
        
    // update users' DB table
        
    query("UPDATE users SET cash = cash + ? WHERE id = ?"$value$_SESSION["id"]);
        
    // insert transaction into DB
        
    query("INSERT INTO history (id, transaction, symbol, shares, price) VALUES (?, ?, ?, ?, ?)"$_SESSION["id"], $transaction$_POST["symbol"], $shares[0]["shares"], $stock["price"]);
        }
        }
        
    // redirect to portfolio
        
    redirect("/");
        }
        else
        {
        
    // query portfolio DB table
        
    $rows query("SELECT * FROM portfolios WHERE id = ?"$_SESSION["id"]);
        
    // create an array to store current user's stock symbols
        
    $stocks = [];
        
    // iterate through each of the current user's stocks
        
    foreach ($rows as $row)
        {
        
    // save each stock symbol
        
    $stock $row["symbol"];
        
    // add each stock symbol to the new array
        
    $stocks[] = $stock;
        }
        
    // render the sell form
        
    render("sell_form.php", ["title" => "Sell Stock""stocks" => $stocks]);
        }
        
    ?>
    The code doesn't update the user's cash as intended, after sales are made. As follows is my code that I thought would have handled the issue:

    PHP Code:
    query("UPDATE users SET cash = cash + ? WHERE id = ?"$value$_SESSION["id"]); 
    Still on the script, the insert transaction into history DB table doesn't seem to work. The history is supposed to display transaction on tran-hist.php. This is what I thought would have handled the transaction insertion into the database:
    PHP Code:
    query("INSERT INTO history (id, transaction, symbol, shares, price) VALUES (?, ?, ?, ?, ?)"$_SESSION["id"], $transaction$_POST["symbol"], $shares[0]["shares"], $stock["price"]); 

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,713
    Thanks
    25
    Thanked 660 Times in 659 Posts
    I also have problems with
    $rows = query("SELECT * FROM portfolios WHERE id = ?", $_SESSION["id"]);

    I don't understand the ',' .
    Shouldn't they all be a dot?
    What is the '?' for?

    There is no query() function in PHP.

    PHP Code:
    Do you mean
    while($rows mysqli_query("SELECT * FROM portfolios  WHERE id = "$_SESSION["id"])){
        echo 
    $rows;

    Same for the other two
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    89
    Thanks
    12
    Thanked 0 Times in 0 Posts
    @sunfighter; thanks for replying. I'm not using MySQLi - just the plain-old MySQL.

    I've found a fix.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •