Form Emails and Databases ids

I send an email from a submit form used to insert data into a database base with a copy of the data in the form to the instigator email address. I want to include in that email the auto increment ID for that record row in the database table.

I cannot work out how to do it!

Any suggestions please

  1. Insert the data in the database
  2. Ask the database what ID the data got
  3. Include that ID in the email, alongside all other data

Thank you rpkamp for a response.

I anticipated that the solution would have to follow that sequence but what I’m really asking is "How"t o do you do it - code wise. I am not a professional coder just a hobbyist and could not fathom the structure of the necessary code


Most of the php database extensions have a function/method to get the last insert id, following the successful execution of an INSERT query. Getting this value just takes researching what the one line function/method call is for the database extension you are using.

Thanks mabismad for the assistance. However I have already tried that approach and is the reason why I posted here because I cannot find the code that can simultaneously post the form data to the database, collect the generated ‘id’ once tht data is inserted, bring it back to the php file and inserted into the that file in order to include it in the email for dispatch at the time upload php code is initiated

I was just looking to see if anyone had a solution to this quandary


Could you show the snippet of code that inserts the data in the database?

^ Agreed - we need to see the code you’re struggling with. While I don’t send emails containing the last insert ID, I do retrieve it and use it. Which specific part are you having trouble with - retrieving the last insert ID, or putting it into an email message?

Okay here is the relevant code from the upload.php file

the upload.php file also includes connection and file upload code but thought that you wouldn’t want that as it seems not relevant


$sql = “INSERT into “tablename” (row ‘name’ in ‘tablename’) VALUES (values from form)”;

if ($conn->query($sql) === TRUE) {
echo "

Your Notice is now displayed on the Regional Notice Board.

} else {
echo "Error: " . $sql . “
” . $conn->error;

EMAIL CODE IN SAME PHP FILE Follows on from above

<?php $sender = " email@form-entered by submitters "; $backup = ""; $subject = "New Notice Posted on - Our Place Website"; // all the following ‘$variable’ are collected from the form $message = "Hi $fname \n You posted the following Notice on the Community Notice Board:\n $comment \n". "With Mobile Phone contact: $mobile\n". "The Notice will stay posted for 30 days - unless you reply sooner, by email, that the Notice is no longer required\n". "After 30 days you will receive an email. You may extend for another 30 days by reply. \n". "Good Luck with your Notice\n". "Warren\n " ; $to = "$email"; $headers = "From: $sender \r\n"; $headers .= "Cc to: $email \r\n"; $headers .= "Bc to: $backup \r\n"; mail($to,$subject,$message,$headers); ?>

As i explained, as the database is getting bigger it is harder to visually locate the post within the DB TABLE for which a user has advised is no longer needed and wants taken down so IF i can include the “id” in the original advise email i can us it to quickly find it straight away

That the purpose of exploring this facility



Are you using mysqli or PDO for your database connection? The way to get the latest insert-id is different.

I’m surprised you don’t run into trouble sending emails using the form-fillers address as your from-address. That basically needs a mail server that supports open relay.

Using mysql

Sorry i dont know how it (the email system) works - it just does

You may be using MySQL as the database engine, but the library of functions that you use to connect to it will be either mysqli or PDO. You’ll be able to tell by looking at the way that the database connection is made. The query() function that you are using is available in both libraries so that doesn’t narrow it down unfortunately.

BTW, please edit your code to display so it’s more easily readable. Put three backticks on a line immediately before the code section, and the same again on a line immediately afterwards. The three backticks need to be on a line on their own, not part of the code.

Immediately after the query you would call for the last insert id. I included both mysqli and PDO versions to your code like so.

if ($conn->query($sql) === TRUE) { 
	//$record_id = $conn -> insert_id;
	$record_id = $conn->lastInsertId();
	echo "Your Notice is now displayed on the Regional Notice Board.";
} else {
	echo "Error: " . $sql . " " . $conn->error;

You could then apply $record_id in your message if that is what you wish.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.