Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $con = mysqli_connect("localhost", "cairil_php", "phpsql");
- // Check if there were any issues connecting to the database
- if ( mysqli_connect_errno() != 0)
- {
- die("An error occurred trying to establish a connection: " .mysqli_connect_error());
- }
- // Select the database to work with.
- $db = mysqli_select_db ($con , "cairil_php");
- ?>
- <html>
- <head>
- <?php
- $pid = $_GET["id"]; //the product id from the URL
- $action = $_GET["action"]; //the action from the URL
- switch($action) { //decide what to do
- case "add":
- $_SESSION['cart'][$pid]++;
- break;
- case "remove":
- $_SESSION['cart'][$pid]--;
- if($_SESSION['cart'][$pid] == 0) unset($_SESSION['cart'][$pid]);
- break;
- case "empty":
- unset($_SESSION['cart']);
- break;
- }
- ?>
- </head>
- <title>CR_cart.php</title>
- <body>
- <center>
- <h1>Welcome <?php echo(($_SESSION['fname']) . " " . ($_SESSION['lname'])) ?></h1>
- <br>
- <h3>Product List</h3>
- <br><br>
- <?php
- if( isset( $_SESSION['cart'])) {
- echo "<table border=\"2\" align=\"center\">";
- echo "<tbody>";
- echo "<tr bgcolor=\"#FE5970\">";
- echo "<th align=\"center\">Product ID</th>";
- echo "<th align=\"center\">Product Name</th>";
- echo "<th align=\"center\">Quantity</th>";
- echo "<th align=\"center\">Remove</th></tr>";
- //iterate through the cart, the $product_id is the key and $quantity is the value
- foreach($_SESSION['cart'] as $pid => $qty) {
- echo $pid;
- echo $qty;
- echo ("<br>line 66<br>");
- $sql = "SELECT 'ProductID', 'ProductName' FROM 'Product' WHERE 'ProductID' = $pid";
- echo $sql;
- echo ("<br>line 71<br>");
- if ($result = mysqli_query($con, $sql))
- {
- // Return the number of rows in result set
- $rowcount=mysqli_num_rows($result);
- echo $rowcount;
- if( $rowcount > 0) {
- list($pid, $pname) = mysql_fetch_row($result);
- echo("<tr><td>");
- echo $pid;
- echo("</td><td>");
- echo $name;
- echo("</td><td>");
- echo $qty;
- echo("</td><td>");
- echo "<a href=\"$_SERVER[PHP_SELF]?action=remove&id=$pid\"><img src=\"/images/b-remove.png\" alt=\"Remove\"></a>";
- echo("</td></tr>");
- }
- }
- else
- {
- echo mysql_error();
- }
- }
- echo "</table>";
- echo "<br><br><table>";
- echo "<tr><td colspan=\"4\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\"><img src=\"/images/b-empty.png\" alt=\"Cart\" onclick=\"return confirm('Are you sure?');\"></a></td>";
- echo "</tr>";
- echo "</table>";
- }else{
- //otherwise tell the user they have no items in their cart
- echo "Cart Empty.";
- }
- ?>
- <br /><br />
- <table>
- <tr>
- <td><a href=CR_products.php><img title"view products" src="/images/b-products.png" alt="View Product" /></a>
- <a href=CR_cart.php><img title"view cart" src="/images/b-shopping.png" alt="View Cart" /></a>
- <a href=CR_order.php><img title"view order" src="/images/b-orders.png" alt="View Orders"/></a></td></tr>
- <tr></tr> <tr></tr>
- <tr><td align="center"><a href=CR_welcome.php><img title="home" src="/images/home_button.png" alt="home" width="150" height="50"/></a> <a href=CR_logout.php><img title"logout" src="/images/exit_button.jpg" alt="Logout" /></a></td></tr>
- </table>
- </center>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement