Jump to content

insert problem


joinx

Recommended Posts

My add to cart is not inserting any data in the table..in fact i am getting errors like

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Apache2.2\htdocs\Jacey\showcart.php:10) in C:\Apache2.2\htdocs\Jacey\showcart.php on line 40

 

Warning: Cannot modify header information - headers already sent by (output started at C:\Apache2.2\htdocs\Jacey\showcart.php:10) in C:\Apache2.2\htdocs\Jacey\showcart.php on line 70

 

Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="description" content="description"/>
<meta name="keywords" content="keywords"/> 
<meta name="author" content="author"/> 
<link rel="stylesheet" type="text/css" href="bntemplate590/images590/default.css"/>
<title>Shopping Cart</title>
<style type="text/css">
<!--
.style4 {font-size: 14pt}
body,td,th {
font-size: 9pt;
}
-->
</style></head>

<body>


<div class="main">

<div class="main_left">

<div class="header">
		<h1 align="center">Jacey Computers</h1>
  </div>

	<div class="link_menu">
		<a href="main.php" accesskey="1">Home</a>
		<a href="category.php" accesskey="2">Product</a>
		<a href="support.php" accesskey="3">Support & Help</a>
		<a href="contact.php" accesskey="4">Contact Us</a>		</div>

	<div class="content">
        <?php require_once('../Connections/JaceyConn.php'); 
session_start();?>
<?php
   // make sure the product id exist
   if (isset($_GET['product_id']) && (int)$_GET['product_id'] > 0) {
      $productId = (int)$_GET['product_id'];
   } else {
      header('Location: product.php');
   }

   // does the product exist ?
mysql_select_db($database_JaceyConn, $JaceyConn);
$sql = "SELECT product_id, product_quantity FROM product WHERE product_id = $productId";
$add = mysql_query($sql, $JaceyConn) or die(mysql_error());
$row_add = mysql_fetch_assoc($add);
$totalRows_add = mysql_num_rows($add);
  

   if ($totalRows_add != 1) {
      // the product doesn't exist
      header('Location: showcart.php');
   } else {
      // how many of this product we
      // have in stock
      $row = mysql_fetch_array($add) ;
      $currentStock = $row['product_quantity'];

      if ($currentStock == 0) {
         // we no longer have this product in stock
         // show the error message
         echo"The product you requested is not available";
         header('Location: showcart.php');
         exit;
      }
   } 

   // current session id
   $sid = session_id();

   // check if the product is already
   // in cart table for this session
    mysql_select_db($database_JaceyConn, $JaceyConn);
   $sql1 = "SELECT product_id
           FROM cart
           WHERE product_id = $productId AND session_id = '$sid'";
	  
$ss = mysql_query($sql1, $JaceyConn) or die(mysql_error());
$row_ss = mysql_fetch_assoc($ss);
$totalRows_ss = mysql_num_rows($ss);
  
  

   if ($totalRows_ss == 0) {
      // put the product in cart table
      $sql2 = "INSERT INTO cart (product_id, quantity, session_id, date)
              VALUES ($productId, 1, '$sid', NOW())";
	$insert = mysql_query($sql2, $JaceyConn) or die(mysql_error());
     
   } else {
      // update product quantity in cart table
      $sql3 = "UPDATE cart 
              SET quantity = quantity + 1
              WHERE session_id = '$sid' AND product_id = $productId"; 

    $update = mysql_query($sql3, $JaceyConn) or die(mysql_error()); 
   }    


?>
        
<table border="2" align="center" >
  <tr> 
   <td align="center">Item</td>
   <td align="center">Unit Price</td>
   <td align="center">Quantity</td>
   <td align="center">Total</td>
</tr>
  </table>



       


       <form id="form1" method="post" action="order.php">
          <input name="order" type="submit" value="Confirm Order" />
             or <a href="category.php"> Continue Shopping.</a>   
        </form>
       
      	  </div>
</div>

</div>
<div class="footer"></div>
</div>
<br style="clear: both;" />

</body>

</html>

 

Link to comment
Share on other sites

here might be the problem, you are missing the single ticks from the php variable $productId

 

try this

 

if ($totalRows_ss == 0) {
      // put the product in cart table
      $sql2 = "INSERT INTO cart (product_id, quantity, session_id, date)
              VALUES ('$productId', 1, '$sid', NOW())";
	$insert = mysql_query($sql2, $JaceyConn) or die(mysql_error());
     
   } else {
      // update product quantity in cart table
      $sql3 = "UPDATE cart 
              SET quantity = quantity + 1
              WHERE session_id = '$sid' AND product_id = '$productId'"; 

    $update = mysql_query($sql3, $JaceyConn) or die(mysql_error()); 
   }    

Link to comment
Share on other sites

You can not start a session after you have output headers. Any output to the browser will output the headers. session_start should be the first thing that you do. Nothing else should be done before you start the session.

 

<?php
 //NO SPACE between the start of the file and the first <?php tag, as that would be output!
 //start the session first
 session_start();
 //output after!
?>

Link to comment
Share on other sites

Single quotes are not needed if said field is numeric.

 

They aren't necessary, but they are recommended as they can protect against some injection attacks, thereby making your script more secure.

Really? Hmm! I shall look into this further. Thanks for the heads up there.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.