我有一个购物车,显示客户选择购买的不同产品。我需要在'order_details'表中使用相同的customer_id将选中的每个产品作为单行插入。
码
<?php
session_start();
@mysql_connect("localhost","root","") or die("Could not connect to database");
@mysql_select_db("bookstore") or die("Could not select database");
$connection = mysqli_connect('localhost', 'root', '', 'bookstore');
include("admin/php/myFunctions.php");
$customer = $_SESSION['id_login'];
$order = $_SESSION['id_login'];
if(!empty($_GET['prodid'])){
$pid = $_GET['prodid'];
$wasFound = false;
$i = 0;
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$_SESSION["cart_array"]=array(0=>array("productID"=>$pid,"quantity"=>1));
}else{
foreach($_SESSION["cart_array"] as $each_product){
$i++;
while(list($key,$value)=each($each_product)){
if($key=="productID" && $value==$pid){
array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$pid,"quantity"=>$each_product ['quantity']+1)));
$wasFound=true;
}
}
}
if($wasFound==false){
array_push($_SESSION["cart_array"],array("productID"=>$pid,"quantity"=>1));
}
}
header("location:shoppingcart.php");
exit();
}
//-------------------------------------------------------------------------------------------------
@$submit = $_POST['btnUpdate'];
if($submit == "Update"){
$x = 0;
//echo $_POST['txtQuan2'];
//echo $_POST['txtHoldProdId0'];
foreach($_SESSION["cart_array"] as $each_product){
$i++;
$quantity = $_POST['txtQuan'.$x];
$prodStock = $_POST['txtHoldQuan'.$x];
$prodAdjustId = $_POST['txtHoldProdId'.$x++];
if($quantity<1){ $quantity = 1; }
if($quantity>$prodStock){ $quantity = $prodStock; }
while(list($key,$value)=each($each_product)){
array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$prodAdjustId,"quantity"=>$quantity)));
}
}
}
//-------------------------------------------------------------------------------------------------
if(!empty($_GET['cid']) || isset($_GET['cid'])){
$removeKey = $_GET['cid'];
if(count($_SESSION["cart_array"])<=1){
unset($_SESSION["cart_array"]);
}else{
unset($_SESSION["cart_array"]["$removeKey"]);
sort($_SESSION["cart_array"]);
}
}
//-------------------------------------------------------------------------------------------------
$cartTitle = "";
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartOutput="<h2 align='center'> Your shopping cart is empty </h2>";
}else{
$x = 0;
$cartTitle .= '<form name="shoppingcart_form" action="shoppingcart.php" method="post" /><table width="700px" cellspacing="0" cellpadding="5">
<tr bgcolor="#CCCCCC">
<th width="220" align="left">Image </th>
<th width="140" align="left">Name </th>
<th width="100" align="center">Quantity </th>
<th width="60" align="center">Stock </th>
<th width="60" align="right">Price </th>
<th width="60" align="right">Total </th>
<th width="90"> </th></tr>';
#Values in here need to go into database "order_details" table
$i = 0;
foreach($_SESSION["cart_array"] as $each_product){
$product_id = $each_product['productID'];
$sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1");
while($row=mysql_fetch_array($sql)){
$prodNo = $row["prod_no"];
$prodID = $row["prod_id"];
$prodName = $row["prod_name"];
$prodPrice = $row["prod_price"];
$prodQuan = $row["prod_quan"];
}
$pricetotal=$prodPrice*$each_product['quantity'];
$cartTotal= number_format($pricetotal+$cartTotal,2);
$cartOutput .= '<tr><td><img style="border: 2px solid;" src="images/product/'.$prodNo.'.jpg" width="150" height="120" /></td>
<td>'.$prodName.'</td>
<td align="center"><input type="hidden" name="txtHoldProdId'.$i.'" value="'.$prodID.'" /><input name="txtQuan'.$i.'" type="text" value="'.$each_product['quantity'].'" style="width: 40px; text-align: center" /> </td>
<td align="center"><input type="hidden" name="txtHoldQuan'.$i.'" value="'.$prodQuan.'" /> '.$prodQuan .' pcs</td>
<td align="right">R '.$prodPrice.'</td>
<td align="right">R '.$pricetotal.'</td>
<td align="center"> <a href="shoppingcart.php?cid='.$i++.'"><img src="images/remove_x.gif" alt="remove" /><br />Remove</a> </td></tr>';
}
$_SESSION['checkoutCartTotal'] = $cartTotal;
$cartOutput .= '<tr>
<td colspan="3" align="right" height="40px">Have you modified your basket? Please click here to <input class="btn_upd" type="submit" name="btnUpdate" value="Update" /> </td>
<td align="right" style="background:#ccc; font-weight:bold"> Total: </td>
<td colspan="2" align="left" style="background:#ccc; font-weight:bold;">R '.$cartTotal.' </td>
<td style="background:#ccc; font-weight:bold"> </td>
</tr>
</table>
<div style="float:right; width: 215px; margin-top: 20px;">
</form>
</div></form>';
}
//---------------------------------------------------
$cTotal = $_SESSION['checkoutCartTotal'];
@$cName = $_POST['cardName'];
@$cNumber = $_POST['cardNum'];
@$cAdress = $_POST['cusAddress'];
@$cCity = $_POST['cusCity'];
@$cEmail = $_POST['cusEmail'];
@$cPhone = $_POST['cusPhone'];
foreach($_SESSION["cart_array"] as $each_product){
$product_id = $each_product['productID'];
$sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1");
while($row=mysql_fetch_array($sql)){
$ProdID = $row["prod_id"];
$prodPrice = $row["prod_price"];
$ProdQuan = $row["prod_quan"];
}
$sqlinsert2 = ("INSERT INTO order_details (`order_id`, `prod_id`, `cus_id`, `quantity`, `price_per_unit`) VALUES ('$order', '$ProdID', '$customer', '$ProdQuan', '$prodPrice')");
}
$sqlinsert = "INSERT INTO tbl_order (`total_price`, `credit_card_number`, `fname`, `email`, `address`, `phone`, `city`,`date_ordered`) VALUES ('$cTotal','$cNumber', '$cName', '$cEmail', '$cAdress', '$cPhone', '$cCity',now())";
if (!mysqli_query($connection, $sqlinsert)) {
die(mysqli_error($connection));
}
$newrecord = "Thank you for making your purchase!";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Great Selling Book Store</title>
<link href="css/slider.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script language="javascript" type="text/javascript">
function clearText(field)
{
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
</head>
<body id="subpage">
<div id="main_wrapper">
<div id="main_header">
<div id="site_title"><h1><a href="#" rel="nofollow">Great Selling book Store</a></h1></div>
<div id="header_right">
<div id="main_search">
<form action="products.php" method="get" name="search_form">
<input type="text" value="Search" name="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field" />
<input type="submit" name="Search" value="" alt="Search" id="searchbutton" title="Search" class="sub_btn" />
</form>
</div>
</div> <!-- END -->
</div> <!-- END of header -->
<div id="main_menu" class="ddsmoothmenu">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="products.php">Books</a></li>
<li><a class="selected" href="shoppingcart.php">Cart</a></li>
<li><a href="about.php">About</a></li>
</ul>
<br style="clear: left" />
</div> <!-- end of menu -->
<div class="cleaner h20"></div>
<div id="main_top"></div>
<div id="main">
<div id="sidebar">
<h3>Categories</h3>
<ul class="sidebar_menu">
<li><a href="index.php?cat=children">Children</a></li>
<li><a href="index.php?cat=Horror">Horror</a></li>
<li><a href="index.php?cat=Thriller">Thriller</a></li>
</ul>
</div> <!-- END of sidebar -->
<div id="content">
<?php echo $cartTitle; ?>
<?php echo $cartOutput; ?>
</div> <!-- end of content -->
<div class="cleaner">
<form method ="post" action="shoppingcart.php">
<input type="hidden" name="submitted" value= "true" />
<fieldset>
<legend>Customer Checkout</legend>
<label>Enter your name as it is on the credit card: <input type="text" name="cardName"></label>
<label>Card Number: <input type="text" name="cardNum"></label>
<label>Adress: <input type="text" name="cusAddress"></label>
<label>City: <input type="text" name="cusCity"></label>
<label>Email: <input type="text" name="cusEmail"></label>
<label>Please, specify your reachable phone number. YOU MAY BE GIVEN A CALL TO VERIFY AND COMPLETE THE ORDER: <input type="text" name="cusPhone"></label>
</fieldset>
<div class="cleaner h50"></div>
<td> <input type="submit" class="more" value="Checkout!"></td>
</form>
</div>
</div> <!-- END of main -->
<?php
echo @$newrecord;
?>
<div id="main_footer">
<div class="cleaner h40"></div>
<center>
Copyright © 2048 DigitalNinja
</center>
</div> <!-- END of footer -->
</div>
<script type='text/javascript' src='js/logging.js'></script>
</body>
</html>
答案 0 :(得分:0)
下面是单个查询中多个插入的示例,它的速度更快,
INSERT INTO example
(example_id, name, value, other_value)
VALUES
(100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2'),
(102, 'Name 3', 'Value 3', 'Other 3'),
(103, 'Name 4', 'Value 4', 'Other 4');
您的代码有多个插入选项:
foreach($_SESSION["cart_array"] as $each_product){
$product_id = $each_product['productID'];
$sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1");
while($row=mysql_fetch_array($sql)){
$Prods .= '('.$order.
','.$row["prod_id"].
','. $order.
','.$row["prod_quan"].
','.$row["prod_price"].
'),';//if text: ',"'.$row["prod_quan"].'"),';
}
$Prods = rtrim($Prods, ',');// this is to remove last comma from multiple inserts
$sqlinsert2 = ("INSERT INTO order_details (`order_id`, `prod_id`, `cus_id`, `quantity`, `price_per_unit`) VALUES $Prods");
mysql_query($sqlinsert2);
}
注意:您没有运行insert的mysql_querry,因此它没有向DB插入数据
警告:您应该将mysqli与parameterized queries一起使用,并且您已经创建了严重的SQL injection bug