Connecting database with my php shopping cart

This is my first post so please correct me if somethings wrong

Basically im creating a shopping cart with the help of a guide but there is no database written in the guide so i’m not sure how to proceed. My database wont work…
Guide:
https://jameshamilton.eu/programming/simple-php-shopping-cart-tutorial

Error message

Index:
[php]<?php session_start(); ?>

Cart <?php //connect to your database here $sql = 'SELECT * FROM books ORDER BY id'; $output[] = '
    '; $output[] = '
  • "'.$row['title'].'" by '.$row['author'].': £'.$row['price'].'
    Add to cart
  • '; //connect mysql mysql_connect($server, $user, $pass) or die ("Sorry, can't conect to mysql."); //select db mysql_select_db($db) or die ("Sorry cant select the db."); ?> <?php $product_id = $_GET[id]; //the product id from the URL $action = $_GET[action]; //the action from the URL //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Error. Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <?php if($_SESSION['cart']) { //if the cart isn't empty //show the cart echo ""; //format the cart using a HTML table //iterate through the cart, the $product_id is the key and $quantity is the value foreach($_SESSION['cart'] as $product_id => $quantity) { //get the name, description and price from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT name, description, price FROM php_shop_products WHERE id = %d;", $product_id); $result = mysql_query($sql); //Only display the row if there is a product (though there should always be as we have already checked) if(mysql_num_rows($result) > 0) { list($name, $description, $price) = mysql_fetch_row($result); $line_cost = $price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo ""; //show this information in table cells echo ""; //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product echo ""; echo ""; echo ""; } } //show the total echo ""; echo ""; echo ""; echo ""; //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation echo ""; echo ""; echo ""; echo "
    $name$quantity X $line_cost
    Total$total
    Empty Cart
    "; }else{ //otherwise tell the user they have no items in their cart echo "You have no items in your shopping cart."; } //function to check if a product exists function productExists($product_id) { //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT * FROM php_shop_products WHERE id = %d;", $product_id); return mysql_num_rows(mysql_query($sql)) > 0; } ?>

    Continue Shopping

    <?php /* products table: CREATE TABLE `products` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR( 255 ) NOT NULL , `description` TEXT, `price` DOUBLE DEFAULT '0.00' NOT NULL , PRIMARY KEY ( `id` ) ); */ ?> [/php]

    poducts.php
    [php]

    Products <?php //connect to your database here //connect to your database here $sql = 'SELECT * FROM books ORDER BY id'; $output[] = '
      '; $output[] = '
    • "'.$row['title'].'" by '.$row['author'].': £'.$row['price'].'
      Add to cart
    • '; //connect mysql mysql_connect($server, $user, $pass) or die ("Sorry, can't conect to mysql."); //select db mysql_select_db($db) or die ("Sorry cant select the db."); ?>
      <?php
      	
      	$sql = "SELECT id, name, description, price FROM php_shop_products;";
      	
      	$result = mysql_query($sql);
      	
      	while(list($id, $name, $description, $price) = mysql_fetch_row($result)) {
      	
      		echo "<tr>";
      		
      			echo "<td>$name</td>";
      			echo "<td>$description</td>";
      			echo "<td>$price</td>";
      			echo "<td><a href=\"cart.php?action=add&id=$id\">Add To Cart</a></td>";
      		
      		echo "</tr>";
      	}
      	
      ?>
      

      View Cart

      [/php]

Daniel,

You’re trying to access values from the database without querying the database…

[php] $sql = ‘SELECT * FROM books ORDER BY id’;
$output[] = ‘

    ’;
    $output[] = ‘
  • "’.$row[‘title’].’" by ‘.$row[‘author’].’: £’.$row[‘price’].‘
    Add to cart
  • ’;[/php]

    You create a SQL Statement, but you don’t make the call to the database to get the results and you need to connect to the database before you make the call, something similar to below should work for you.

    [php] //connect mysql

    mysql_connect($server, $user, $pass) or die (“Sorry, can’t conect to mysql.”);

    //select db

    mysql_select_db($db) or die (“Sorry cant select the db.”);
    $row = mysql_query($sql);
    $sql = ‘SELECT * FROM books ORDER BY id’;
    $output[] = ‘

      ’;
      $output[] = ‘
    • "’.$row[‘title’].’" by ‘.$row[‘author’].’: £’.$row[‘price’].‘
      Add to cart
    • ’;
      [/php]

You might want to replace anywhere it says “mysql” with “mysqli”. It is slightly better (the ‘i’ on the end is for improved). It really doesn’t matter.
What Topcoder said was almost perfect, but he said mysql_query before he defined the query. I am copping pasting and changing his so that the query is defined before it was used. Besides that some error reporting would be useful.[php]
//connect mysql

mysql_connect($server, $user, $pass) or die (“Sorry, can’t conect to mysql.”);

//select db

mysql_select_db($db) or die (“Sorry cant select the db.”);
$sql = ‘SELECT * FROM books ORDER BY id’;
$row = mysql_query($sql) or die(“Ut oh! Something went wrong. The error was: " . mysql_error();
$output[] = ‘

    ’;
    $output[] = '
  • ”’.$row[‘title’].’" by ‘.$row[‘author’].’: £’.$row[‘price’].‘
    Add to cart
  • ’;
    [/php]

    I hope this helps!

@Russ

You’re right, Cut and Paste error, thanks for correcting me.

Sponsor our Newsletter | Privacy Policy | Terms of Service