简体   繁体   中英

shopping cart with PHP and connected to the MySQL

Good evening, I am currently working on the implementation of a shopping cart through PHP and MySQL, I am getting the following error at line 172 onwards, I have been looking at the quotes but I cannot find the problem, what could be the solution to this?

ERROR : Parse error: syntax error, unexpected 'img' (T_STRING), expecting ';' or ',' in B:\XAMPP\htdocs\dashboard\PHP_Assessments\Sport_Cars\cart.php on line 171     


 <div class="container" style="width:700px;">  
                    <h3 align="center">Simple PHP Mysql Shopping Cart</h3><br />  
                    <?php  
                    $sql = "SELECT * FROM sport_cars.cars ORDER BY carID ASC";  
                     //prepared statement
                     $statement = $conn->prepare($sql);
                     $statement->execute();
                     $result = $statement->fetchAll();
                     $statement->closeCursor(); 
                     foreach($result as $row):
                        echo "<div class='col-md-4'>";
                        echo "<form method='post' action='cart.php?action=add&id=" .$row['carID']. ' ">";
                        echo "<div><img class='img-responsive' src= " .'view/images/'. $row['Photo'] . " /><br/> ";  
                        echo  "<h4 class='text-info'> . $row['carName'] . </h4> ";
                        echo "<h4 class='text-danger'>.$row['carPrice']; . "</h4> ";  
                        echo "<input type='text' name='quantity' class='form-control' value='1' /> ";  
                        echo "<input type='hidden' name='hidden_name' value=.' ['carPrice']; '. /> ";  
                        echo  "<input type='hidden' name='hidden_price' value=.' $row['carPrice']; '. /> ";  
                        echo  "<input type='submit' name='add_to_cart' style='margin-top:5px;' class='btn btn-success' value='Add to Cart' /> ";
                        echo "</div></form></div>";
                    endforeach;
                    if($result > 0)  
                    {  
                         while($row = $statement -> fetchAll())  
                         {  
                    ?>  
                    <div class="col-md-4">  
                         <form method="post" action="cart.php?action=add&id=<?php echo $row["carID"]; ?>">  
                              <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">  
                                   <img src="<?php echo $row["Photo"]; ?>" class="img-responsive" /><br />  
                                   <h4 class="text-info"><?php echo $row["carName"]; ?></h4>  
                                   <h4 class="text-danger">$ <?php echo $row["carPrice"]; ?></h4>  
                                   <input type="text" name="quantity" class="form-control" value="1" />  
                                   <input type="hidden" name="hidden_name" value="<?php echo $row["carPrice"]; ?>" />  
                                   <input type="hidden" name="hidden_price" value="<?php echo $row["carPrice"]; ?>" />  
                                   <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" />  
                              </div>  
                         </form>  
                    </div>  
                    <?php  
                         }  
                    }  
                    ?>  
                    <div style="clear:both"></div>  
                    <br />  
                    <h3>Order Details</h3>  
                    <div class="table-responsive">  
                         <table class="table table-bordered">  
                              <tr>  
                                   <th width="40%">Item Name</th>  
                                   <th width="10%">Quantity</th>  
                                   <th width="20%">Price</th>  
                                   <th width="15%">Total</th>  
                                   <th width="5%">Action</th>  
                              </tr>  
                              <?php   
                              if(!empty($_SESSION["shopping_cart"]))  
                              {  
                                   $total = 0;  
                                   foreach($_SESSION["shopping_cart"] as $keys => $values)  
                                   {  
                              ?>  
                              <tr>  
                                   <td><?php echo $values["carName"]; ?></td>  
                                   <td><?php echo $values["quantity"]; ?></td>  
                                   <td>$ <?php echo $values["carPrice"]; ?></td>  
                                   <td>$ <?php echo number_format($values["quantity"] * $values["carPrice"], 2); ?></td>  
                                   <td><a href="cart.php?action=delete&id=<?php echo $values["carID"]; ?>"><span class="text-danger">Remove</span></a></td>  
                              </tr>  
                              <?php  
                                        $total = $total + ($values["quantity"] * $values["carPrice"]);  
                                   }  
                              ?>  
                              <tr>  
                                   <td colspan="3" align="right">Total</td>  
                                   <td align="right">$ <?php echo number_format($total, 2); ?></td>  
                                   <td></td>  
                              </tr>  
                              <?php  
                              }  
                              ?>  
                         </table>  
                    </div>
             </div>

I have been changing this cart from a mysqli version of it into pdo and I am having some trouble to make it work, I hope this can be fixed.

Thanks for your help.

It's PHP syntax error. You ca refer to the screenshot below for some error as example. You need to make sure there is no error first before getting it works在此处输入图像描述

NO NO NO! When you use

echo "<div></div>"; You never Put a  " Inside of two "" You can only use ''

echo "<div class=""></div>";Wrong
echo "<div class=''></div>";Correct

-- LETS LOOK AT THIS LINE --

echo "<form method='post' action='cart.php?action=add&id=" .$row['carID']. ' ">";

You are doing this wrong. First of all This is in Double Quotes So you don't need the. $row['carId'] .

The fixed code would be:

    $id = $row['id'];
    echo "<form method=\"Post\" action=\"car.php?action=add&id=$id\" >";

You would need to Change your whole code:(

You had some errors in your foreach loop containing $result so try this:

foreach($result as $row){ 

    echo "<div class='col-md-4'>";
    echo "<form method='post' action='cart.php?action=add&id=" .$row['carID'].  ">";
    echo "<div><img class='img-responsive' src= " .'view/images/'. $row['Photo'] . " 
    /> 
    <br/> ";  
    echo  "<h4 class='text-info'>" . $row['carName'] . "</h4> ";
    echo "<h4 class='text-danger'>".$row['carPrice'] . "</h4> ";  
    echo "<input type='text' name='quantity' class='form-control' value='1' /> ";  
    echo "<input type='hidden' name='hidden_name' value='" . $row['carPrice'] . "'/> 
    ";  
    echo  "<input type='hidden' name='hidden_price' value='" . $row['carPrice']  . 
    "'/> ";  
    echo  "<input type='submit' name='add_to_cart' style='margin-top:5px;' class='btn 
    btn- 
    success' value='Add to Cart' /> ";
    echo "</div></form></div>";

}

You could try using printf to help with readability of the code. http://php.net/printf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM