garevn Posted May 23, 2011 Share Posted May 23, 2011 Hello I have a cart and i want to insert some users data into mysql in 2 different tables.My basic cart code is this <?php$cartOutput="";$cartTotal="";if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){ $cartOutput="<h2 align='center'>your shopping cart is empty</h2>";}else{ $i=0; foreach($_SESSION["cart_array"]as $each_item){ $item_id=$each_item['item_id']; $sql=mysql_query("SELECT productid,productname,price,image FROM products WHERE productid='$item_id' LIMIT 1"); while($row= mysql_fetch_array($sql)){ $productname=$row['productname']; $price=$row['price']; $image=$row['image']; $productid=$row['productid']; } $pricetotal=number_format($price*$each_item['quantity'],2,".",""); $cartTotal=number_format($pricetotal+$cartTotal,2,".",""); $cartOutput.="<tr>"; $cartOutput.='<td><img src="images/site pic 2/mysql/'.$image.'" width="45" height="45" /></td>'; $cartOutput.='<td>'. $productid.'</td>'; $cartOutput.='<td>'. $productname.'</td>'; $cartOutput.='<td>$'. $price.'</td>'; $cartOutput.='<td><form action="mycart.php" method="post"> <input name="quantity" type="text" value="'. $each_item['quantity'].'" size="1" maxlength="2" /> <input name="adjustBtn'.$item_id.'" type="submit" value="change" /> <input name="item_to_adjust" type="hidden" value="'.$item_id.'" /> </form></td>'; //$cartOutput.='<td>'. $each_item['quantity'].'</td>'; $cartOutput.='<td>$'. $pricetotal.'</td>'; $cartOutput.='<td><form action="mycart.php" method="post"> <input name="deleteBtn'.$item_id.'" type="submit" value="X" /> <input name="index_to_remove" type="hidden" value="'.$i.'" /> </form> </td>'; $cartOutput.='</tr>'; $i++; } $cartTotal="<div align='center'>Cart Total: $".$cartTotal."</div>";}?> When the user wish to buy the products i display him a form to complete some data like address/credit card/payment method/and card type. i have method post and action orderdata.php.I Tried to pass some values with sessions but i am not really experienced.My current orderdata.php looks like this <?phpsession_start();$productid=$_SESSION['productid'] ;$pricetotal=$_SESSION['pricetotal'] ;$price=$_SESSION['price'] ;$cartTotal=$_SESSION['cartTotal'] ;$each_item=$_SESSION['quantity'] ; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mypc", $con); $sql="INSERT INTO sales (address, paymethod, creditcard,credittype,totalprice) VALUES ('$_POST[address]','$_POST[RadioGroup1]','$_POST[pass]', '$_POST[type]','$cartTotal')"; $sql="INSERT INTO orders (productid, quantity, unitprice, pricetotal) VALUES ('$productid','$each_item','$price','$pricetotal')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 what is the problem you are facing now with this? Link to comment Share on other sites More sharing options...
garevn Posted May 23, 2011 Author Share Posted May 23, 2011 at the moment when i submit the form it only inserts values at the orders table and the quantity is 0 instead of the real one Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 $sql="INSERT INTO sales (address, paymethod, creditcard,credittype,totalprice) VALUES ('$_POST[address]','$_POST[RadioGroup1]','$_POST[pass]', '$_POST[type]','$cartTotal')"; $sql="INSERT INTO orders (productid, quantity, unitprice, pricetotal) VALUES ('$productid','$each_item','$price','$pricetotal')";It is overwriting the $sql. so when you executing it only the last $qry is beeing executed .$sql="INSERT INTO sales (address, paymethod, creditcard,credittype,totalprice) VALUES ('$_POST[address]','$_POST[RadioGroup1]','$_POST[pass]', '$_POST[type]','$cartTotal')"; // execute the query here mysql_query() $sql="INSERT INTO orders (productid, quantity, unitprice, pricetotal) VALUES ('$productid','$each_item','$price','$pricetotal')";// again execute another mysql_query() Link to comment Share on other sites More sharing options...
garevn Posted May 23, 2011 Author Share Posted May 23, 2011 My current code is <?phpsession_start();$productid=$_SESSION['productid'] ;$pricetotal=$_SESSION['pricetotal'] ;$price=$_SESSION['price'] ;$cartTotal=$_SESSION['cartTotal'] ;$each_item=$_SESSION['quantity'] ;$con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mypc", $con); $sql1="INSERT INTO sales(address, paymethod, creditcard,cardtype )VALUES('$_POST[address]','$_POST[RadioGroup1]','$_POST[pass]','$_POST[type]')";mysql_query($sql1,$con);$sql2="INSERT INTO orders (productid, quantity, unitprice, pricetotal, quantity)VALUES('$productid','$each_item','$price','$pricetotal','$each_item')";mysql_query($sql2,$con);mysql_close($con) ?> I have the following problems--> I have 2 radio button options, i only insert data in the 1st table when i choose the second option.--> The quantity i insert into the table two is always 0 Link to comment Share on other sites More sharing options...
garevn Posted May 23, 2011 Author Share Posted May 23, 2011 I almost complete everything tnx Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.