简体   繁体   English

如何使用ID相同的变量值使用PHP INSERT将数组存储到mysql

[英]How to store an array to mysql using PHP INSERT with same variable value for id

So I'm trying to store values for my _SESSION array which is making the cart within my website work. 因此,我正在尝试存储_SESSION数组的值,这将使购物车在我的网站上正常工作。 I'm having trouble INSERTING the same array to my database - I guess I have same id for multiple arrays so that's why it's not working. 我在向数据库中插入相同的数组时遇到了麻烦-我猜我对多个数组有相同的ID,因此这是行不通的。 That's my guess. 那是我的猜测。 I hope someone can help me. 我希望有一个人可以帮助我。

Here's my first attempt at trying. 这是我第一次尝试。

 <? $con = mysql_connect('bluecubecreatives.net', 'bluecu6_user', '*****'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bluecu6_databasename", $con); $itemsupdate = "INSERT INTO orderitems (ID, ORDER_ID, ITEM_ID, NAME, QUANTITY, SIZE, FOR, PRICE, TOTAL) VALUES (NULL, '$id', '$item_stock_id', '$item_name', '$item_quantity', '$item_size', '$item_for', '$item_price', '$item_total');"; mysql_query($itemsupdate); ?> 

and here's my second try at fixing it. 这是我第二次尝试修复它。

 $additems = array ('ID' => NULL, 'ORDER_ID' => $item_id, 'ITEM_ID' => $item_stock_id, 'NAME' => $item_name, 'QUANTITY' => $item_quantity, 'SIZE' => $item_size, 'FOR' => $item_for, 'PRICE' => $item_price, 'TOTAL' => $item_total); $update = "INSERT INTO orderitems (ID, ORDER_ID, ITEM_ID, NAME, QUANTITY, SIZE, FOR, PRICE, TOTAL) VALUES"; $it = new ArrayIterator( $additems ); // a new caching iterator gives us access to hasNext() $cit = new CachingIterator( $it ); // loop over the array foreach ( $cit as $value ) { // add to the query $sql .= "('".$cit->key()."','" .$cit->current()."')"; // if there is another array member, add a comma if( $cit->hasNext() ) { $sql .= ","; } } 

HERE's the full code where I'm trying to push the SESSION items and the billing + shipping details to my database. 这里是完整的代码,我正在尝试将SESSION项目以及帐单+运送详细信息推送到数据库中。

 $connection = mysql_connect('bluecubecreatives.net', 'bluecu6', '****'); $selected = mysql_select_db("bluecu6", $connection) or die("Hello ONE, Sorry! This AI system couldn't connect to your database."); if (!$connection) { die('Could not connect: ' . mysql_error()); } if (isset($_GET['id'])) { $id = $_GET['id']; } else { $id = 1; } if (isset($_GET['size'])) { $size = $_GET['size']; } else { $size = "Standard"; } if (isset($_GET['quantity'])) { $quantity = $_GET['quantity']; $cart_update = 1; } else { $quantity = 0; $cart_update = 0; } if (isset($_GET['for'])) { $for = $_GET['for']; } else { $for = "All Purpose"; } if (isset($_GET['action'])) { $action = $_GET['action']; } else { $action="empty"; } $data = mysql_query("SELECT * FROM products WHERE ID= '" . $id . "';" ,$connection) or die(mysql_error()); $val = mysql_fetch_array($data); if ($action=="add") { $newitem = array ('id' => $id , 'name' => $val['PRO_NAME'], 'quantity' => $quantity, 'size' => $size,'for' => $for,'price' => $val['PRO_RS']); $_SESSION['item'][$id] = $newitem; } $_SESSION['quantity'] = $_SESSION['quantity'] + $cart_update; $in_cart = $_SESSION['quantity']; $_SESSION['cart_total'] = $in_cart; if ($_SESSION['cart_total'] <=0) { $_SESSION['cart_total'] = 0; } if ($id!=0) { switch($action) { case "view": if (isset($_SESSION['cart'][$id])) { $_SESSION['cart'][$id]; } else { $_SESSION['cart'][$id]=0; } break; case "refresh"; session_destroy(); break; case "delete"; if ($_SESSION['cart_total']==1) { $_SESSION['cart_total']=0; unset($_SESSION['item']); session_destroy(); } else if ($_SESSION['cart_total']>1) { $_SESSION['quantity']--; $_SESSION['cart_total']--; unset($_SESSION['item'][$id]); } break; case "add": if (isset($_SESSION['cart'][$id])) { $_SESSION['cart'][$id]++; } else { $_SESSION['cart'][$id]=1; } break; case "remove"; if (isset($_SESSION['cart'][$id])) { $_SESSION['cart'][$id]--; if ($_SESSION['cart'][$id]==0) { unset($_SESSION['cart'][$id]); $_SESSION['cart_total']--; } if ($_SESSION['cart'][$id]==-1) { unset($_SESSION['cart'][$id]); $_SESSION['cart_total']--; } } break; case "empty"; unset($_SESSION['cart']); $_SESSION['cart_total'] = 0; break; } } if($_POST['ADD_TOCONFIRM']){ $shipping_option = $_POST['shipping_option'];$payment_option = $_POST['payment_option'];$discount_val = $_POST['discount_val']; $bname = $_POST['name'];$baddress = $_POST['address'];$bcity = $_POST['city'];$bprovince = $_POST['province'];$bzip = $_POST['zip']; $bcountry = $_POST['country'];$bcontact = $_POST['contact'];$bemail = $_POST['email']; $sname = $_POST['sname'];$saddress = $_POST['saddress'];$scity = $_POST['scity'];$sprovince = $_POST['sprovince'];$szip = $_POST['szip']; $scountry = $_POST['scountry'];$scontact = $_POST['scontact'];$semail = $_POST['semail']; $status = "Pending"; // foreach ( $_SESSION['item'] AS $data ) { $xo = mysql_query("SELECT * FROM products WHERE ID= '" . $data['id'] . "';" ,$connection) or die(mysql_error()); $yo = mysql_fetch_array($xo); $l_cost = $data['price'] * $data['quantity']; $t_cost = $t_cost + $l_cost; $t_bill = $t_cost; $item_id = (int) $id; $item_stock_id = (int) $yo['ID']; $item_name = $yo['PRO_NAME']; $item_size = $data['size']; $item_for = $data['for']; $item_quantity = (int) $data['quantity']; $item_price = (int) $yo['PRO_RS']; $item_total = (int) $l_cost; $con = mysql_connect('bluecubecreatives.net', 'bluecu6', '***'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bluecu6", $con); $itemsupdate = "INSERT INTO orderitems (ID, ORDER_ID, ITEM_ID, NAME, QUANTITY, SIZE, FOR, PRICE, TOTAL) VALUES (NULL, '$item_id', '$item_stock_id', '$item_name', '$item_quantity', '$item_size', '$item_for', '$item_price', '$item_total');"; mysql_query($itemsupdate); echo '<br />'; echo $item_id. '<br />'; echo $item_stock_id. '<br />'; echo $item_name. '<br />'; echo $item_size. '<br />'; echo $item_for. '<br />'; echo $item_quantity. '<br />'; echo $item_price. '<br />'; echo $item_total. '<br />'; } if (($shipping_option!=="")&&($payment_option!=="")&&($bname!=="")&&($baddress!=="")&&($bcity!=="")&&($bprovince!=="")&&($bzip!=="")&&($bcountry!=="")&&($bcontact!=="")&&($bemail!=="")) { $con = mysql_connect('bluecubecreatives.net', 'bluecu6', '*****'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bluecu6", $con); $query = "INSERT INTO orders (ORDER_ID, SHIP_OPTION, PAY_OPTION, DISCOUNT, NAME, ADDRESS, CITY, PROVINCE, ZIP, COUNTRY, CONTACT, EMAIL, NAME_SHIP, ADDRESS_SHIP, CITY_SHIP, PROVINCE_SHIP, ZIP_SHIP, COUNTRY_SHIP, CONTACT_SHIP, EMAIL_SHIP, STATUS) VALUES (NULL, '$shipping_option', '$payment_option', '$discount_val', '$bname', '$baddress', '$bcity', '$bprovince', '$bzip', '$bcountry', '$bcontact', '$bemail', '$sname', '$saddress', '$scity', '$sprovince', '$szip', '$scountry', '$scontact', '$semail' , '$status');"; mysql_query($query); echo '<br />'; echo "success". '<br />'; echo '<br />'; echo $shipping_option. '<br />'; echo $payment_option. '<br />'; echo $discount_val. '<br />'; echo '<br />'; echo $bname. '<br />'; echo $baddress. '<br />'; echo $bcity. '<br />'; echo $bprovince. '<br />'; echo $bzip. '<br />'; echo $bcountry. '<br />'; echo $bcontact. '<br />'; echo $bemail. '<br />'; echo '<br />'; echo $sname. '<br />'; echo $saddress. '<br />'; echo $scity. '<br />'; echo $sprovince. '<br />'; echo $szip. '<br />'; echo $scountry. '<br />'; echo $scontact. '<br />'; echo $semail. '<br />'; echo '<br />'; echo $total_bill. '<br />'; //echo mysql_query($newitem). '<br />'; foreach ( $_SESSION['item'] AS $item ) { echo 'id: ' . $item['id'] . ' - '; echo 'name: ' . $item['name'] . ' - '; echo 'quantity: ' . $item['quantity'] . ' - '; echo 'size: ' . $item['size'] . ' - '; echo 'price: ' . $item['price'] . ' - '; echo 'for: ' . $item['for'] . '<br />'; $line_cost = $item['price'] * $item['quantity']; $total_cost = $total_cost + $line_cost; $total_bill = $total_cost; } echo '<br />'; echo '<br />'; echo $total_bill; echo '<br />'; echo '<br />'; mysql_close($con); } else { echo "please fill all options"; } } 

suppose if you have following array, 假设您有以下数组,

$additems = array ('ID' => NULL, 'ORDER_ID' => $item_id, 'ITEM_ID' => $item_stock_id, 'NAME' => $item_name, 'QUANTITY' => $item_quantity, 'SIZE' => $item_size, 'FOR' => $item_for, 'PRICE' => $item_price, 'TOTAL' => $item_total);    

you can perform your query like 您可以像执行查询

$update =  "INSERT INTO orderitems (ORDER_ID, ITEM_ID, NAME, QUANTITY, SIZE, FOR, PRICE, TOTAL) VALUES ($additems['ORDER_ID'],$additems['ITEM_ID'],$additems['NAME'],$additems['QUANTITY'],$additems['SIZE'],$additems['FOR'],$additems['PRICE'],$additems['TOTAL'])";

try this, this should work. 试试这个,这应该工作。 feel free to ask if you got any problem with this. 随时询问您是否对此有任何疑问。

So this is what was wrong with my code. 所以这是我的代码出了什么问题。 I extracted the INSERT code from mysql and configured it in my PHP file. 我从mysql中提取了INSERT代码,并在我的PHP文件中对其进行了配置。 All of a sudden, it all started working. 突然,一切都开始了。

 $itemupdate = "INSERT INTO `orderitems`(`SESSION_ID`, `ORDER_ID`, `ITEM_ID`, `NAME`, `QUANTITY`, `SIZE`, `FOR`, `PRICE`, `TOTAL`, `NOTES`) VALUES ('$session_no','$item_id','$item_stock_id','$item_name','$item_quantity','$item_size','$item_for','$item_price','$item_total','notes');"; mysql_query($itemupdate); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM