Issue with adding item to cart

Hi,

I was wondering if anyone could help me. Currently when i select the button add to cart the current page (Car information page) refreshes and does not go to the cart page. I am unsure why i cannot go to the cart page and why its refreshing the car information page. Please could you identify any issues with my code and correct me if i have made any mistakes I will be much appreciated to the help given thanks. :slight_smile:

Car Price Page(Where all cars are displayed (Works)
[php]

<?php require "dbc.php"; $query = "SELECT * from cars"; $result = mysql_query ($query); $rCount = mysql_num_rows($result); $aresult = array(); $aresult = mysql_fetch_array($result); while($row = mysql_fetch_array($result)){ $CID = $row ["id"]; echo'
'; echo '
'.$row ["Car_Name"]; echo '
Price: &pound'.$row ["Price"]; echo '
'; ''; } ?>

[/php]

Car Information Page( When the user selects a car it only displays an individual car with on this page with the add to cart button)

[php]

<?php session_start(); $carid = $_GET['id']; require_once "dbc.php"; $result = mysql_query('SELECT * FROM cars WHERE id = "'.$carid.'"'); $rCount = mysql_num_rows($result); if($row = mysql_fetch_array($result)){ do{ $CID = $row ["id"]; echo '
'; echo '
'; echo '
'.$row ["Car_Name"]; echo '
Quantity: '.$row ["Quantity"]; echo '
Price: ÂŁ'.$row ["Price"]; //CHANGE add this line --v echo ''; echo '
'; echo '
'; echo '




'; }while($row = mysql_fetch_array($result)); }else{print"Sorry, no records where found!";} ?>[/php]

Cart Page (Currently not working as when i select the button add to cart from prev page it doesn’t show this page

[php]

<?php session_start(); require_once ("dbc.php"); //if product exists in command line, add it to cart @ $cid = $_GET['cid']; //checks if command line has product added if($cid){ //checks if cart is empty & creates it if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); $_SESSION['items'] = 0; $_SESSION['total_price'] = '0.00'; } //cart array contains car id & quantity if (isset($_SESSION['cart'] [$cid] )) $_SESSION['cart'] [$cid]++; else $_SESSION['cart'] [$cid] = 1; } //checks if command line has product updated if(isset($_POST['update'])){ foreach ($_SESSION['cart'] as $carid => $qty){ //value from text box name if($_POST[$carid]<='0') unset($_SESSION['cart'] [$carid]); else $_SESSION['cart'] [$carid] = $_POST[$carid]; } } //displays car session_start(); if ($_SESSION['cart']){ //echo '

Cart Contents

'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; $_SESSION['total_price'] = '0.00'; $_SESSION['items'] = 0; //place lines of products in page foreach ($_SESSION['cart'] as $carid => $qty){ //Connect to your Database Server with your credentials require "dbc.php"; $query = 'SELECT * FROM cars WHERE id = "'.$carid.'"'; $result = mysql_query($query); $catNo = mysql_num_rows($result); $aresult = array(); $product = mysql_fetch_array($result); $tprice = $qty*$product['Price']; echo ''; echo ''; echo ''; $_SESSION['items'] += $qty; $_SESSION['total_price'] += $tprice; //close the connection mysql_close($conn); } echo ''; echo ''; echo ''; echo ''; echo ''; echo '
TitleQuantityPrice
'; echo $product['Car_Name']; echo ''; //name of textbox=carid and value=qty echo ''; echo 'ÂŁ'; echo $tprice; echo '
'; echo 'Total Qty: '.$_SESSION['items'].'Total: ÂŁ'.$_SESSION['total_price'].'
'; echo ''; echo '

'; echo ''; echo ''; //echo '
'; }else{ //if no product in cart echo '
'; echo '

You currently have no products in Shopping Cart.

'; echo '

'; //echo '
'; } ?>

[/php]

Hi , I’m quite new to PHP but I think its refreshing the same page as you have a submit button but no form tags so I looks like its not being sent anywhere ,

This wil tell the page to send the information to your cart page . Hope this helps

Hi, thanks for your response but unfortunately in the car information page i do have form action “cart.php” :(… but even then it still doesn’t go to that page…

Sorry just looked at your code again , I missed it , you getting your info from the url as your using get ?

Yep, so initially the user select a car image say for instance (porsche) the user is then taken to the car information screen using the car id from the image of that car… on this page the user has add to cart button… and here is the issue its that communication between pressing the add to cart to going to the cart page which is the issue… ?? i don’t know if that helps?? let me know if you need any more information…

Ok cool , after another look I think its your if statment above the output as your saying if row = mysql_fetch_array
Do what’s below … But that condition isn’t set(row doesnt =mysql_fetch_array) ( car info page )

sorry if this is a dumb question, but is your cart.php file in the same directory as your car info page and named cart.php? Just a habit from IT work and programming for longer hours, I like to check if the basics are correct

Hi, Yeah all the files are in the same folder in htdocs. i was wondering would you be able to show me a simple checkout system that basically when the user selects an image similar to car price page it goes to an information page (car info page) which shows the add to cart button and then when the user selects add to cart it goes to the cart.php effectively being a checkout where validation is checked to see if the user has registered or is logged on… i would be really grateful if you could show me an example… i’m scratching my head to where this issue lies… thankss :slight_smile:

to add to albertle5’s basic questions are there any other forms within the page that might not be closed?

hi, i was wondering what you meant by closed??

a form element missing a tag

Hi,
im soo glad you mentioned that i managed to get to the next page but now im having a new issue…

Notice: Undefined variable: conn in C:\xampp\htdocs\Prestige Supercar Website\cart.php on line 143

Warning: mysql_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Prestige Supercar Website\cart.php on line 143

Cart.php line 143 = mysql_close($conn);

was missing an <a href end tag …

I’m assuming none of the above files are cart.php as none of which contain near 143 lines, as such its hard to pinpoint exactly, however judging by the warning(which aren’t necessarily fatal and can be disabled in the servers php settings file) you’re $conn is not defined by mysql_connect()… without inspection of code theres a few options here… disable warning/notices, connect $conn to a connection to the database, or if you aren’t using mysql connections simply change line 143 to “mysql_close();”

Sponsor our Newsletter | Privacy Policy | Terms of Service