0

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
?>
cloudseeker
  • 245
  • 1
  • 2
  • 13
  • 1
    Not an answer, but your queries aren't in an `else` for connecting to your database. Also, please be aware that your code is **vulnerable** to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection), as you don't use [**prepared statements**](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Also ensure that your database user only has the [**required privileges**](https://en.wikipedia.org/wiki/Principle_of_least_privilege). You can refer to [**this post**](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for further information :) – Obsidian Age Oct 08 '17 at 21:32
  • Thanks @ObsidianAge with relation to the sql injection side of things, the filtering is all done pre insert but just not listed here. The user for this database function has create privileges only. Thanks for the information though. :-) – cloudseeker Oct 08 '17 at 21:36
  • This is not a duplicate. One question refers to the function itself, the other question related to an unknown number of rows being inserted. If you are going to mark things as duplicate, please read the question before making it off. – cloudseeker Oct 09 '17 at 03:11

0 Answers0