0

I am working on a simple form only 2 fields. Name and Email. What I have succeded in doing so far is.

  1. I have the form send the data to a mysql database.
  2. I can query the database in total and also query the database based on a form entry.
  3. I have tested and I can send email using a basic mail script or from a form.

Where I am running to an issue is I need the data to also be sent to the submitter along with a third field from the database whish is the auto-encrement primary key as their member number. My question is how can I send email based on the database entry.

ie; Thank you "Submitter Name" for joining your Member ID is "Primary Key". This is my current code.

Index.html

<form method="post" action="doit.php">
    Name<input name="Name" type="text" /><br>
    Email<input name="Email" type="text" /><br>
    <input name="Submit" type="submit" value="submit" />
</form>

doit.php

<?php
$con = mysqli_connect('localhost','DB_Username','DB_Password');

if(!$con)
{
    echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'DB_Name'))
{
    echo 'Database Not Selected';
}
$Name = $_POST['Name'];
$Email = $_POST['Email'];

$sql = "INSERT INTO Table_name (Name,Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))
{
    echo 'Not Inserted';
}
else
{
    echo 'Inserted';
}
header ("refresh:2; url=auto-response.php");


?>

auto-response.php

<?php
if ($_SERVER["REQUEST_METHOD"}=="POST") {
    $connnection=mysqli_connect("localhost","DB_username","DB_password","DB_Name");
    // Check connection
    if ($connection) {
        echo "connection successfull! <br>;"
    } else {
        die("connection failed. Reason ". mysqli_connect_err())
}
{

$Email=$_POST["Email"];
$Name=$_POST["Name"];
$ID=$_POST["ID"];

$sql="SELECT ID,Email,Name FROM Table_Name WHERE Email='".$Email."'";
$result = mysqli_query($connection,$sql);

$to      = 'Email';
$subject = 'Thank you For Joining';
$message = 'Thank you for joining "Email" Your Member ID is "ID"';
$headers = 'From: dsarmy@thedarksideshop.com' . "\r\n" .
    'Reply-To: dsarmy@thedarksideshop.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 
Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
PCMedicJAX
  • 79
  • 8
  • We need the problem and the line of code you have tried that isn't working, we have no idea what your code looks like. – MinistryOfChaps Sep 23 '17 at 16:22
  • To ask an On Topic questions, please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [What topics to avoid](https://stackoverflow.com/help/dont-ask) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [take the tour](http://stackoverflow.com/tour) – RiggsFolly Sep 23 '17 at 16:29
  • Maybe you should show us the HTML for your form and script that insert the new client into your database because this code is basically nonsense – RiggsFolly Sep 23 '17 at 16:48
  • I added the rest of the code. it consists of 3 pages. the form, the insertion page, and the non-functional auto-response page. I just need to pass the data to the auto-response page and populate the proper places.I hope this clarifies what I need. – PCMedicJAX Sep 23 '17 at 17:32
  • Why refresh to another page ? And why place a check of $_POST there ? Obviously your third page won't work. – Shahbaz A. Sep 24 '17 at 04:18

1 Answers1

0

Mysql has its own function which will give you id. While you already have the name at the time of insert.

// Using the below line right after your insert you can get the primary id.
$id = $connection->insert_id; // Get latest inserted id.

Hope it helps.

Edit: Based on the edit you made i have arranged your code to how it should work. Try this now.

Index.html

<form method="post" action="doit.php">
    Name<input name="Name" type="text" /><br>
    Email<input name="Email" type="text" /><br>
    <input name="Submit" type="submit" value="submit" />
</form>

doit.php

$con = mysqli_connect('localhost','DB_Username','DB_Password');

if(!$con)
{
    echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'DB_Name'))
{
    echo 'Database Not Selected';
}

$Name = $_POST['Name'];
$Email = $_POST['Email'];

$sql = "INSERT INTO Table_name (Name,Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))
{
    echo 'Not Inserted';
}
else
{
    echo 'Inserted';
    $id = $con->insert_id; // Get latest inserted id.

    $to      = $Email;
    $subject = 'Thank you For Joining';
    $message = 'Thank you for joining "'.$Name.'" Your Member ID is "'.$id.'"';
    $headers = 'From: dsarmy@thedarksideshop.com' . "\r\n" .
        'Reply-To: dsarmy@thedarksideshop.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
}
Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
  • Ermmm `mysql_insert_id();` would be great IF the OP was inserting a new row, but that is a SELECT he, and you are doing I am afraid – RiggsFolly Sep 23 '17 at 16:43
  • Oh great. Since he said he is inserting i edited the code without looking at the query. My bad – Shahbaz A. Sep 23 '17 at 16:46
  • Did the fix. Accroding to what he needs. – Shahbaz A. Sep 23 '17 at 16:50
  • Its not really an answer now, based on the script he show us, which I admit is basically nonsense – RiggsFolly Sep 23 '17 at 16:51
  • I added the rest of the code. it consists of 3 pages. the form, the insertion page, and the non-functional auto-response page. I just need to pass the data to the auto-response page and populate the proper places.I hope this clarifies what I need. – PCMedicJAX Sep 23 '17 at 17:21
  • That did it Thanks works perfectly for your assistance. – PCMedicJAX Sep 26 '17 at 18:20