Jump to content

Help Debug PHP/MySQL Part 2

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Fatal error: Function name must be a string in C:\website\webdev\robert.harrison\ITEC 3280\Coffee Shop\shoppingCart.php on line 33

<?php

session_start();

$conn = mysqli_connect(//deleted for obvious reasons);

if (!$conn) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
        }
        
else{
    $sku = $_SESSION['sku'];
    $quantity = $_POST['quantity'];
    $userid = $_SESSION['name'];

    if($_SESSION['name'] == ''){
        $_SESSION['signedIn'] = 'no';
        header('Location: signIn.php');
    }

    else if($_POST['update'] == 'Update Cart'){

    echo "Attempting to update cart.";

        $sql = "SELECT sku, quantity FROM cart WHERE userid = '".$userid."';";
        
        $rs = mysqli_query($conn, $sql);
        //$num_rows = mysqli_num_rows($rs);
        
        while($row = mysqli_fetch_assoc($rs)){
        
            //The compiler does not like the following statement
            //but how do i get the new quantity for each item
            $quantity = $_POST($row['sku']);
            
            echo "<p>Inside loop to update cart<br/>
                  $quantity = ".$quantity."</p>";
            
            $sql = "UPDATE CART
                    SET quantity = '".$quantity."'
                    WHERE userid = '".$userid."' AND sku = '".$row['sku']."';";
            $rs = mysqli_query($conn, $sql);
            
            $sql = "SELECT coffee_sku, coffee_name, coffee_price, quantity
                FROM coffee
                JOIN CART ON coffee_sku = sku
                WHERE userid = '".$userid."';";
            $rs = mysqli_query($conn, $sql);
            
            echo "<form action'shoppinCart.php' method='post'>";
            
            echo "<table name = 'cart'>
                <tr>
                    <th>Coffee Name</th><th>Coffee Price</th><th>Quantity</th>
                </tr>";
                
            if (mysqli_num_rows($rs)== 0){
                        echo "No items are currently in your shopping cart.";
            }

            while($row = mysqli_fetch_assoc($rs)){
                echo "<tr>";
                echo "<td>". $row['coffee_name'] . "</td>";
                echo "<td>$" . $row['coffee_price'] . "</td>";
                echo "<td><input type='text' size='2' name='".$row['coffee_sku']."' value='" . $row['quantity'] . "'></td>";
                echo "</tr>";                
            }
            
            echo "<tr>
                    <td></td><td></td><td><input type='submit' name='update' value='Update Cart'</td>
                  </tr>";
            
            echo "</table>";
            
            echo "Displaying " . mysqli_num_rows($rs) . " items.";
            
            mysqli_close($conn);
            
            echo "</form>";
                
            }

    }

    else{

        
        $sql = "INSERT INTO CART
                VALUES ('".$userid."', '".$sku."', '".$quantity."');";
        $rs = mysqli_query($conn, $sql); 
        
        if(mysqli_affected_rows($conn) == 0){
            echo "Insert failed!";
        }
        
        $sql = "SELECT coffee_sku, coffee_name, coffee_price, quantity
                FROM coffee
                JOIN CART ON coffee_sku = sku
                WHERE userid = '".$userid."';";
        $rs = mysqli_query($conn, $sql);
        
        echo "<form action'shoppinCart.php' method='post'>";
        
        echo "<table name = 'cart'>
            <tr>
                <th>Coffee Name</th><th>Coffee Price</th><th>Quantity</th>
            </tr>";
            
        if (mysqli_num_rows($rs)== 0){
                    echo "No items are currently in your shopping cart.";
        }

        while($row = mysqli_fetch_assoc($rs)){
            echo "<tr>";
            echo "<td>". $row['coffee_name'] . "</td>";
            echo "<td>$" . $row['coffee_price'] . "</td>";
            echo "<td><input type='text' size='2' name='".$row['coffee_sku']."' value='" . $row['quantity'] . "'></td>";
            echo "</tr>";                
        }
        
        echo "<tr>
                <td></td><td></td><td><input type='submit' name='update' value='Update Cart'</td>
              </tr>";
        
        echo "</table>";
        
        echo "Displaying " . mysqli_num_rows($rs) . " items.";
        
        mysqli_close($conn);
        
        echo "</form>";

    }
}
    
?>


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
These syntax errors are easy mistakes, I would recommend you get a good syntax highlighter that at least shows line numbers (i.e. Geany) to get a better grasp on what is wrong. On line 35 you have this:
$quantity = $_POST($row['sku']);
Remember the error is trying to tell you a hint, in this case you can see you are trying to use $_POST like a function, and in this case a function cannot be a variable name (like $_POST()).

I had spotted your comment question:

code said:

//The compiler does not like the following statement
//but how do i get the new quantity for each item
$quantity = $_POST($row['sku']);
-- but I do not understand what you are trying to do. Are you trying to get quantity from $_POST or from your query? I do not understand your application logic.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
My logic is that I am trying to update the quantity of each item in the shopping cart. My input boxes are dynamically named the same as the coffee_sku. When the user hits the "update" button, then each new quantity is passed through the post method.