Okay, I know that there is a bit of information out there in relation to using mysqli_insert_id($conn)
to get the ID of the last record.
My question is actually a two part relating to the same piece of code and is about something that isn't in any of this information (from what I could find).
Part 1 - Am I getting the last insert id that has just been created by this user, or if there are 5 million (ignore the performance side for a sec) people entering records at the same time, is there going to be a chance of it picking up another user's inserted record number?
Do I then need to go further and check for the last record + by the user "...."?
Part 2 - How is it best to handle submissions with an unknown length of rows, say in the case of invoice items in an invoice, someone may have 1 or 2 or 22... How is it best to handle the numerous unknown length submissions? It is currently set to handle 3 items at a time, but gets a little interesting when 4+ is required. DB type is InnoDB and data is submitted through a + / - dynamic line items entry form.
Any guidance would be appreciated.
Currently this is my code (been tidied up for submission here):
<?php
$con=mysqli_connect($DBServer,$DBUser,$DBPass,$DBName);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Fetching Values from URL
$logdate = $getserverlogdate;
$logtime = $getserverlogtime;
$clientid="96";
$uid="23";
$invdate="2017-11-05";
$invtime="11:45";
$invcreated="2017-11-05 11:45:06";
$invnotes="Bla Bla Test Note";
$invitemname0 = "Test Item Name 0";
$invitemtax0 = "10.00%";
$invitemcost0 = "16.95";
$invitemname1 = "Test Item Name 1";
$invitemtax1 = "10.00%";
$invitemcost1 = "25.20";
$invitemname2 = "Test Item Name 2";
$invitemtax2 = "10.00%";
$invitemcost2 = "5.00";
//Insert query to generate the invoice shell
$query = mysqli_query($con,"insert into testtblinvoices(user_id, inv_date, inv_time, inv_created, meal_notes) values ('$clientid', '$invdate', '$invtime','$invcreated','$invnotes')");
// Get record # of last insert
$invrecord = mysqli_insert_id($con);
// Invoice Items Submission
$query = mysqli_query($con,"insert into testtblinvitems(invoice_id, client_id, inv_item_name, inv_item_tax, inv_item_cost) values ('$invrecord', '$clientid', '$invitemname0','$invitemtax0','$invitemcost0')");
$query = mysqli_query($con,"insert into testtblinvitems(invoice_id, client_id, inv_item_name, inv_item_tax, inv_item_cost) values ('$invrecord', '$clientid', '$invitemname1','$invitemtax1','$invitemcost1')");
$query = mysqli_query($con,"insert into testtblinvitems(invoice_id, client_id, inv_item_name, inv_item_tax, inv_item_cost) values ('$invrecord', '$clientid', '$invitemname2','$invitemtax2','$invitemcost2')");
// Mark entry in log
$query = mysqli_query($con,"insert into testtbllogactions(user_id, related_record, log_action, log_action_date, log_action_time) values ('$uid', '$invrecord', 'invoice creation','$logdate','$logtime')");
// Ajax submission return message
echo "Your record was succesfully added.";
mysqli_close($con); // Connection Closed
?>