Jump to content

Help Debug PHP/MySQL

- - - - -

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

#1
Blue Indian

Blue Indian

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
I am getting the following error and I can't seem to figure out why:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\website\webdev\robert.harrison\ITEC 3280\Coffee Shop\shoppingCart.php on line 32

Many thanks!!

<?php

session_start();

$conn = mysqli_connect(//deleted these parameters for obvious reasons);
$sku = $_SESSION['sku'];
$quantity = $_POST['quantity'];
$userid = $_SESSION['name'];

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

else if($_POST['submit'] == '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)){
    
        $quantity = $_POST[$row['sku']];
        
        $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
Line 32 contains this query:
        $sql = "UPDATE CART
                SET quantity = '".$quantity."'
                WHERE userid = '".$userid."' AND sku = '".$row['sku']"';";

You can see there is a missing fullstop after $row['sku'] to connect the next string.
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
Many thanks!:c-thumbup:

I see it now. I couldn't see it last night.

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I suggest using a text editor using syntax highlighting helping a lot to find such errors :-)
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

Orjan said:

I suggest using a text editor using syntax highlighting helping a lot to find such errors :-)
That's the thing, syntax highlighters only help with multiple tokens, a single period often is not possible to warn about, much hair pulling!
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.