Re: [PHP-DB] Shopping cart

[email protected] (danaketh)
Newsgroups php.db
Message-ID <[email protected]>
Hi Vee,

    You have it all in that error :) You're calling function on 
non-object. Look into the basket.php. On the lines where you call the 
MySQL query you have $con->query($sql) but there is no decalaration of 
$con object anywhere before.

    I think you have an MySQL layer included (mysql.class.php) but you 
haven't it declared. You just made a new relation in $con. Instead of 
calling $con->query() you have to call mysql_query(). You also have some 
mistakes in query ;)

$sql = 'SELECT * FROM spirits ORDER BY id';
$query = mysql_query($sql);
...

    Look at the PHP documentation how to work with mysql_ functions. Or 
provide us with link to the MySQL layer you're using - that 
mysql.class.php - so we can help you with using that. I think there will 
be something like

$con = new MySQL('host', 'user', 'pass', 'db');

    Then your syntax will work. However without knowing what layer it 
is, I can't tell you the right syntax for it ;)


Vernon St Croix napsal(a):
> Hi,
>
> I am pretty new to PHP and I am trying to create a shopping cart. 
>
> I keep on getting the below error when trying to show the shopping list. 
>
> Any guidance that can be provided will be very much appreciated
>
> Fatal error: Call to a member function query() on a non-object in C:\wamp\www\draft\basket.php on line 36
>
> mysql_connect.php
> <?php
> $con = mysql_connect("localhost","root","");
> if (!$con)
>   {
>   die('Could not connect: ' . mysql_error());
>   }
> mysql_select_db("rum", $con);
> ?>
>
> basket.php
> <?php 
>  include("mysql.class.php");
>  include ("header.php");
>  include ("mysql_connect.php");
>   include ("functions.php");
>  ?>
>  <div id="shopping">
> <h2>Rum Basket</h2>
> <?php
>     echo writeCart();
>     ?>
>  </div>
> <div id="rumlist">
>   <h2>Rum on Offer</h2>
>   <?php
>  
>  $sql= 'SELECT * FROM spirits BY id';
>   $result = $con->query($sql);
>  $output[]= '<ul>';
>  while ($row = $result->fetch()) {
>  $output[] = '<li>'.$row['name'].': &pound;'.$row['price'].'<br/><a href="cart.php?action=add&id=
>   '.$row['id'].'">Add to Cart</a></li>';
>     }
>     $output[] = '</ul>';
>   echo join ('', $output);
>    ?>
>   </div>
>
> </div>
> <?php
>  include("footer.html");
>
> ?>
>
>
> cart.php
>
> <?php 
>
>  include ("header.php");
>  
>  include ("mysql_connect.php");
>  
>  include ("functions.php");
>  
>
>
> $cart = $_SESSION['cart'];
>
>
> if(isset($_GET["action"]))
> { $action = $_GET["action"]; }
> else
> { $action = ""; }
>
>
> switch ($action) {
>  case 'add':
>   if ($cart) {
>    $cart .= ','.$_GET['id'];
>   } else {
>    $cart = $_GET['id'];
>   }
>   break;
>  case 'delete':
>   if ($cart) {
>    $items = explode(',',$cart);
>    $newcart = '';
>    foreach ($items as $item) {
>     if ($_GET['id'] != $item) {
>      if ($newcart != '') {
>       $newcart .= ','.$item;
>      } else {
>       $newcart = $item;
>      }
>     }
>    }
>    $cart = $newcart;
>   }
>   break;
>  case 'update':
>  if ($cart) {
>   $newcart = '';
>   foreach ($_POST as $key=>$value) {
>    if (stristr($key,'qty')) {
>     $id = str_replace('qty','',$key);
>     $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
>     $newcart = '';
>     foreach ($items as $item) {
>      if ($id != $item) {
>       if ($newcart != '') {
>        $newcart .= ','.$item;
>       } else {
>        $newcart = $item;
>       }
>      }
>     }
>     for ($i=1;$i<=$value;$i++) {
>      if ($newcart != '') {
>       $newcart .= ','.$id;
>      } else {
>       $newcart = $id;
>      }
>     }
>    }
>   }
>  }
>  $cart = $newcart;
>  break;
> }
> $_SESSION['cart'] = $cart;
>
> ?>
>
> <div id="shopping">
>
> <h2>Rum Basket</h2>
>
> <?php
> echo writeCart();
> ?>
>
> </div>
>
> <div id="contents">
>
> <h2>Please Check Quantities...</h2>
>
> <?php
> echo showCart();
> ?>
>
> <p><a href="basket.php">Back to Rum List</a></p>
>
> </div>
>
> </div>
>
>
> <?php
>  include("footer.html");
>
> ?>
>
> functions.php
>
> <?php
> function writeCart() {
>  $cart = $_SESSION['cart'];
>  if (!$cart) {
>   return '<p>There is no alcohol in your Rum Basket</p>';
>  } else {
>   // Parse the cart session variable
>   $items = explode(',',$cart);
>   $s = (count($items) > 1) ? 's':'';
>   return '<p>There are<a href="cart.php">'.count($items).' item'.$s.' in your rum basket</a></p>';
>  }
> }
>   
>      function showCart() {
>  $cart = $_SESSION['cart'];
>  if ($cart) {
>   $items = explode(',',$cart);
>   $contents = array();
>   foreach ($items as $item) {
>    $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
>   }
>   $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
>   $output[] = '<table>';
>   foreach ($contents as $id=>$qty) {
>    $sql = 'SELECT * FROM spirits WHERE id = '.$id;
>    $result = $con->query($sql);
>    $row = $result->fetch();
>    extract($row);
>    $output[] = '<tr>';
>    $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
>    $output[] = '<td>'.$name.'</td>';
>    $output[] = '<td>&pound;'.$price.'</td>';
>    $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" ></td>';
>    $output[] = '<td>&pound;'.($price * $qty).'</td>';
>    $total += $price * $qty;
>    $output[] = '</tr>';
>   }
>   $output[] = '</table>';
>   $output[] = '<p>Grand total: &pound;'.$total.'</p>';
>   $output[] = '<div><button type="submit">Update cart</button></div>';
>   $output[] = '</form>';
>  } else {
>   $output[] = '<p>You shopping cart is empty.</p>';
>  }
>  return join('',$output);
> }
>
> ?>
>
>
>
>
> Many Thanks
>
> Vee
>   

-- 

S pozdravem

Daniel Tlach
Freelance webdeveloper

Email: [email protected]
ICQ: 160914875
MSN: [email protected]
Jabber: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.