hi guys, i have a problem with the checkout page of my shopping cart. it can't insert into table multiple records. this is my code: <?php if ($checkout == "yes") { $sql = "INSERT INTO header (fullname, address, city, tk, phone, kinito, pliromi, remarks, ordate, sessionid) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"; $sqlRes = mysql_query(sprintf($sql,$_POST['fullname'],$_POST['address'],$_POST['city'],$_POST['tk'],$_POST['phone'],$_POST['kinito'],$_POST['pliromi'],$_POST['remarks'],$_POST['ordate'],$sessionid), $con); if(!$sqlRes){ echo "Error Inserting Information into Header<br>"; } else { $sql2 = "SELECT * FROM cart WHERE sessionid='$sessionid'"; $sql2Res = mysql_query($sql2, $con); } if(!$sql2Res){ echo "Error Selecting from Cart<br>"; } else { $sql3 = "SELECT orderid FROM header WHERE sessionid='$sessionid' ORDER BY orderid desc LIMIT 0, 1"; $sql3Res = mysql_query($sql3, $con); } if(!$sql3Res){ echo 'Error Selecting from Header table<br>'; } else { $sql4 = "INSERT INTO details (orderid, code, qty, pricer) VALUES ('".$orderid."', '".$code."', '".$qty."', '".$pricer."')"; $sql4Res = mysql_query($sql4, $con); } if(!$sql4Res){ echo 'Error Inserting into Details table'; } else { $sqlDel = "DELETE FROM cart WHERE sessionid='$sessionid'"; mysql_query($sqlDel, $con); } if (!$sqlDel){ die('Error: ' . mysql_error()); } else { echo "Record ok"; mysql_close($con); } ?><?php } else { ?><the form><?php } ?> PHP: all SQL queries are working fine except of SQL4 (INSERT INTO details). on this table must insert some fields from the cart table (code, qty, pricer) and one field from header (orderid).
Are you properly escaping special chars like single quotes in your variables? What does mysql_error() return?
No offense, but you need to look into php5 standards. Your code has too much wrong to list here. No disrespect intended, though.
This should work : $sql4 = "INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer')"; $sql4Res = mysql_query($sql4); PHP: i tested it myself on my local PC , if it still doesnt work, pm me your php version and mysql version and the error that it triggers thanks
i'm using the WampServer Version 2.0 (Apache version: 2.2.8 | PHP version: 5.2.5 | mySQL version: 5.0.51a). it doesn't appears any error. i want to insert into details all records from the cart. these are the multiple records. but i want to insert the same orderid for each record. i'm getting the orderid from the header.
hmm you mean , insert $sql4 = "INSERT INTO `details` (`code`, `qty`, `pricer`) VALUES ('$code', '$qty', '$pricer') WHERE `orderid` = '$orderid'"; $sql4Res = mysql_query($sql4); PHP: this code will match the database's tables's "orderid" field with the variable "orderid" . suppose you have $orderid = 5 it will insert the $code, $qty, $pricer into that field where orderid is 5 is this what you want? if no, pm me the code, and explain more clearly what you are trying to do. thanks
ohh you mean, $orderid is in header ?? like $orderid is SESSION variable you mean ? if yes, then here you go: $sql_for_first_order = "INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer')"; $sqlfirst = mysql_query($sql_for_first_order); // for further orders $sql_for_futher_orders= "INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer') WHERE `orderid` = '$orderid'"; $sqlfurther = mysql_query($sql_for_futher_orders); PHP: now, if you are using session to track the orderid for client's cart, use before mysql queries: $_SESSION['orderid'] must be defined already, let us make it current date and time: if ($_SESSION['orderid'] == "" || empty($_SESSION['orderid'])) { $_SESSION['orderid'] = $orderid = date(Ymd).time(); $orderid = $_SESSION['orderid']; } PHP: so that, our client's order id will be "200805271211903361" now, we use mysql query to insert data to mysql tables. use the query code given above AND I couldnt understand what you meant by this : "i want for each cart record to set the same orderid from header" but I got some general idea, it should be : if ($_SESSION['orderid'] == "" || empty($_SESSION['orderid'])) { $_SESSION['orderid'] = $orderid = date(Ymd).time(); $orderid = $_SESSION['orderid']; } $sql = "INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer')"; $sqlr = mysql_query($sql_for_first_order); PHP: THIS CODE WILL SET SAME ORDERID FOR EACH CART ORDER BY SAME CLIENT Hope this helps..
i don't use any session for the orderid. is only an auto_increanment field in header table. nothing else. when selects the records (products) from the cart, i want the orderid to insert in the details with each product code, qty, pricer. do you anderstand now?
nope, not fully understood except that you were talking about table "header", which i thought was page headers anyways, i still cant understand what you are trying to say ( sorry for that ) tell me one of these options : A . You want orderid to be automatically increased for each order B . You want orderid to be same for all orders C . You want to grab the orderid from table "header" and use it to insert the details, like if orderid in header is 6, orderid in products to be inserted should also be 6 D . You want orderid in header table to match with product code and insert it with qty price etc if you have any other option, please post them here I will be glad to help you out
$query_id = mysql_query("SELECT `orderid` FROM `header`"); $query_r = mysql_fetch_assoc($query_id); $orderid_init = $query_r['orderid']; $_SESSION['orderid'] = $orderid_init; $orderid = $_SESSION['orderid']; $sql = "INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer')"; $sqlr = mysql_query($sql_for_first_order); PHP: okay, this code will fetch the orderid from table "header", and will assign that to $orderid, which will be inserted into the table "details" along with the other details ( qty, price etc) I hope thats the thing you wanted
this is the checkout in ASP code. i had the idea from this checkout ASP page. <%sqlheader="insert into header (fullname,address,city,phone,mobile,postcode,ordate,payment,sessionid) VALUES ('"&fullname&"','"&address&"','"&city&"','"&phone&"','"&mobile&"','"&postalcode&"','"&ordate&"','"&payment&"','"&session.SessionID&"')" con2.execute sqlheader sqlselfromCart="select * from cart where sessionid='"&session.sessionid&"'" set ordersRS=con.execute(sqlselfromcart) sqlselfromheader="select orderid from header where sessionid='"&session.SessionID&"' order by orderid desc" set orderidRS=con2.execute(sqlselfromheader) orderid=orderidRS("orderid") 'response.write(orderid) WHILE NOT ordersRS.EOF code=ordersRS("code") qty=ordersRS("qty") pricer=ordersRS("pricer") sqlInsertIntoDetails="insert into details (orderid,code,qty,pricer) VALUES ('"&orderid&"','"&code&"','"&qty&"','"&pricer&"')" con2.execute sqlInsertIntoDetails response.Write("record ok!")%><br> HTML:
er.. could you please tell me whats not working with my code ?? ?? All of the time I tried to help you has gone waste you mean ? huh ? If you cant tell (in english) what you want to do, then how can you expect someone to help you ?
i want to "transfer" the products from the temporary table (cart) to details table (not all the fields. only the code, qty, pricer). i want also to "transfer" the orderid from the header table to details table. for each record from cart i want the orderid (from header) to be the same in details table.
okay, so you want this : transfer code, qty, pricer from table "cart" to table "details" and orderid from table "header" and orderid should be same for all records. If I am right (I am sure I am), then here is the code you are looking for : <?php $query_id = mysql_query("SELECT `orderid` FROM `header`"); $query_r = mysql_fetch_assoc($query_id); $orderid = $query_r['orderid']; $cart_prods = mysql_query("SELECT `code`, `qty`, `pricer` FROM `cart`"); while ($cart_r = mysql_fetch_assoc($cart_prods)) { echo "Transfering $code , $qty , $pricer From Table 'cart' to Table 'details' with orderid as $orderid<br />\n"; mysql_query("INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer')"); } ?> PHP: It will automatically transfer all the records of code, qty and pricer from table cart to details, and will put same orderid for each of them (which will be fetched from table header)
my friend we have done the half job. in the details table is inserting the orderid only. this is the echo message: can you see if is any problem whith the cart selection? this is the entire php code: <?php $sql = "INSERT INTO header (fullname, address, city, tk, phone, kinito, pliromi, remarks, ordate, sessionid) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"; $sqlRes = mysql_query(sprintf($sql,$_POST['fullname'],$_POST['address'],$_POST['city'],$_POST['tk'],$_POST['phone'],$_POST['kinito'],$_POST['pliromi'],$_POST['remarks'],$_POST['ordate'],$sessionid),$con); $query_id = mysql_query("SELECT orderid FROM header WHERE sessionid='$sessionid' ORDER BY orderid desc LIMIT 0, 1"); $query_r = mysql_fetch_assoc($query_id); $orderid = $query_r['orderid']; $cart_prods = mysql_query("SELECT code, qty FROM cart"); while ($cart_r = mysql_fetch_assoc($cart_prods)) { echo "Transfering $code, $qty, From Table 'cart' to Table 'details' with orderid as $orderid<br />\n"; mysql_query("INSERT INTO details (orderid, code, qty) VALUES ('$orderid', '$code', '$qty')"); } $sqlDel = "DELETE FROM cart WHERE sessionid='$sessionid'"; mysql_query($sqlDel, $con); echo "Record ok"; mysql_close($con); ?> PHP:
oh yes wait, made a little mistake.. forgot to assign variables.. there you go : <?php $query_id = mysql_query("SELECT `orderid` FROM `header`"); $query_r = mysql_fetch_assoc($query_id); $orderid = $query_r['orderid']; $cart_prods = mysql_query("SELECT `code`, `qty`, `pricer` FROM `cart`"); while ($cart_r = mysql_fetch_assoc($cart_prods)) { $code = $cart_r['code']; $qty = $cart_r['qty']; $pricer = $cart_r['pricer']; echo "Transfering $code, $qty, $pricer From Table 'cart' to Table 'details' with orderid as $orderid<br />\n"; mysql_query("INSERT INTO `details` (`orderid`, `code`, `qty`, `pricer`) VALUES ('$orderid', '$code', '$qty', '$pricer')"); } ?> PHP: try it now, it must work now. if it doesnt, then you probably havent got anything in your cart table..