ok i have 98% of the code done but struggling on the last bit. I'm creating a ordering system and i'm taking values from one page, sending them to a php page and inserting them into a mysql db like this:
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Cannot connect to mysql server");
mysql_select_db($dbname) or die("Cannot select database");
if (isset($_POST['data']) && is_array($_POST['data'])) {
foreach ($_POST['data'] as $row => $data) {
$result = mysql_query("INSERT INTO orders (id,project_ref,supp_short_code,om_part_no,description,quantity,cost_of_items,cost_total) VALUES('', '".$data['project_ref']."', '".$data['supp_short_code']."', '".$data['om_part_no']."', '".$data['description']."', '".$data['quantity_input']."', '".$data['cost_of_items']."', '".$data['cost_total_td']."') ") or die(mysql_error());
}
}
Which is great (and i know i'm not protecting against sql injection, etc) but there could be multiple rows being inserted into the db using the above code, but what i need is for the user to later be able to pull back an order based on one id number. So how do i insert an id number into this insert query so it matches all rows and is definitely unique in the db and ideally auto-incrementing (i'm aware this is going to be tricky considering theres multiple rows!)
Add a column to your orders
table called BatchID
. Then, generate a GUID in PHP, and modify your INSERT
statement accordingly.
See here for details on generating a GUID.
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
Assuming that your 'id' field is meant to hold the autoincrementing id for each order, you will want to add a new insert prior to your current insert statement that will just add a record to a table with an auto-incrementing primary key. Then take this new primary key and associate it with your current table instead of the current 'id' or in addition to the current 'id' field.
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Cannot connect to mysql server");
mysql_select_db($dbname) or die("Cannot select database");
if (isset($_POST['data']) && is_array($_POST['data'])) {
result = mysql_query("INSERT INTO orders_to_order SET order_primary = NULL");
$orderId = mysql_insert_id();
foreach ($_POST['data'] as $row => $data) {
$result = mysql_query("INSERT INTO orders (id,project_ref,supp_short_code,om_part_no,description,quantity,cost_of_items,cost_total) VALUES(".$orderId.", '".$data['project_ref']."', '".$data['supp_short_code']."', '".$data['om_part_no']."', '".$data['description']."', '".$data['quantity_input']."', '".$data['cost_of_items']."', '".$data['cost_total_td']."') ") or die(mysql_error());
}
}
What I'd do in a case like this is have two tables, orders
and order_detail
The orders
table would have your grand totals
, order date
, customer id
, etc.
The order_detail
table would have an entry for each individual item.. part_no
, quantity
, cost
, extended_cost
but would link back to the orders
table by an order_id
.
You'd end up selecting like:
SELECT * FROM orders JOIN order_detail ON orders.order_id = order_detail.order_id WHERE orders.order_id = '1'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.