Sending ID number to their email after filling out form or use trigger in mysql

Refresh

April 2019

Views

23 time

1

I have looked on other questions and nothing quite fits, I am trying to adapt parts to my code I have built so far, but the new stuff I am adding I get errors, I am sure I'm not far off. I want to learn to do it so I will keep trying.

I don't expect someone to do it for me, I want to do it and understand how it's working to constantly improve.

So my MySQL database has an ID field auto_increment and the clients details.

After my customer fills in the form, I would like an email with their ID number sent to their email address they have just put in the form. (make sense?)

Can I do this as the customer only gets an ID number in the database after filling out the form?

OK! so writing this I've just realized that maybe I can create a trigger inside MySQL database to send an email with the ID number to the client once the details enter the database.

I just googled that and not sure if its possible

So far my code is this and I built it and it works a treat! a long process but I'm getting there and loving it!

<?php

require 'connection.php';
$conn    = Connect();
$date    = $conn->real_escape_string($_POST['u_date']);
$name    = $conn->real_escape_string($_POST['u_name']);
$company    = $conn->real_escape_string($_POST['u_company']);
$tel    = $conn->real_escape_string($_POST['u_tel']);
$mtel    = $conn->real_escape_string($_POST['m_tel']);
$email   = $conn->real_escape_string($_POST['u_email']);
$occupation   = $conn->real_escape_string($_POST['occupation']);
$hear   = $conn->real_escape_string($_POST['hear']);
$used   = $conn->real_escape_string($_POST['used']);
$signature    = $conn->real_escape_string($_POST['signature']);

$query   = "INSERT into tb_cform (u_date,u_name,u_company,u_tel,m_tel,u_email,occupation,hear,used,signature)     VALUES('" . $date . "','" . $name . "','" . $company . "','" . $tel . "','" .     $mtel . "','" . $email . "','" . $occupation . "','" . $hear . "','" . $used .     "','" . $signature . "')";
$success = $conn->query($query);


if (!$success) {
die("Couldn't enter data: ".$conn->error);

}

echo "<p><center>Thank you <strong>$name</strong>, your message has been submitted to us. Check your email for your ID number</center></p>";

$conn->close();

?>

Thank you in advance!

2 answers

0

When you insert a record and want to know the ID value the easiest way is get the value of LAST_INSERT_ID(). With mysqli this is actually simple using insert_id:

$id = $conn->insert_id;
0

So I found a way round the ID number, but all I want is for this to send an email to me and to the one put in the contact form, its working putting it in the database but I need a little help on the email sending part of the code. Thanks

 <?php

 require 'connection.php';

 $conn    = Connect();
 $pass    = $conn->real_escape_string($_POST['pass']);
 $date    = $conn->real_escape_string($_POST['u_date']);
 $name    = $conn->real_escape_string($_POST['u_name']);
 $company    = $conn->real_escape_string($_POST['u_company']);
 $tel    = $conn->real_escape_string($_POST['u_tel']);
 $mtel    = $conn->real_escape_string($_POST['m_tel']);
 $email   = $conn->real_escape_string($_POST['u_email']);
 $occupation   = $conn->real_escape_string($_POST['occupation']);
 $hear   = $conn->real_escape_string($_POST['hear']);
 $used   = $conn->real_escape_string($_POST['used']);
 $signature    = $conn->real_escape_string($_POST['signature']);


 $query   = "INSERT into tb_cform (pass,u_date,u_name,u_company,u_tel,m_tel,u_email,occupation,hear,used,signature) VALUES('" . $pass . "','" . $date . "','" . $name . "','" . $company . "','" . $tel . "','" . $mtel . "','" . $email . "','" . $occupation . "','" . $hear . "','" . $used . "','" . $signature . "')";


 $success = $conn->query($query);


 $header = "From: $email\n" . "Reply-To: $email\n";
 $subject = "Inquiry Received";
 $email_to = "[email protected]";
 $message = "Full Name: $name\n"
           . "Word: $pass\n"
           . "Email: $email\n";
mail($email_to, $subject, $message, $header);



if (!$success) {
die("Couldn't enter data: ".$conn->error);

 }

 echo "<p><center>Thank you <strong>$name</strong>, your message has been 
 submitted to us.</center></p>";

 $conn->close();

 ?>