使用 PDO/MySQL 发送电子邮件并将自动递增编号附加到电子邮件

Sending and email with the auto Increment number attached to the email using PDO/MySQL

你好我最喜欢的人!

我正在尝试在提交表格后发送一封电子邮件,电子邮件中附有 AUTO INCREMENT 编号,因为 AUTO INCREMENT 编号是客户的工作卡参考编号。到目前为止,我已经成功地创建了插入脚本,它可以将数据完美地插入到数据库中,还可以发送电子邮件。但不会将 AUTO INCREMENT 编号附加到电子邮件中。在我的 MySQL 数据库中,INT(11) AUTO INCREMENT 主键是“job_number”。

这是我的插入页面:

<form action="addnewrepairprocess.php" method="POST">
         
         <div class="form-group">
      <label for="date">Date</label>
      <input type="date" name="date" id="date" class="form-control" placeholder="Job Card Date">
    </div>


         <div class="form-group">
      <label for="client_full_name">Client Full Name</label>
      <input type="text" name="client_full_name" class="form-control" id="client_full_name" placeholder="Mr. Laptop Man">
    </div>
    
     <div class="form-group">
      <label for="client_email">Client Email Address</label>
      <input type="email" name="client_email" class="form-control" id="client_email" placeholder="example@live.co.za">
    </div>
    
    <div class="form-group">
      <label for="client_phone">Client Phone Number</label>
      <input type="text" name="client_phone" class="form-control" id="client_phone" placeholder="071 984 5522">
    </div>
    
     <div class="form-group">
     <label for="item_for_repair">Item For Repair</label>

<select name="item_for_repair" id="item_for_repair">
  <option value="Laptop">Laptop</option>
  <option value="Desktop">Desktop</option>
  <option value="Television">Television</option>
  <option value="Washing Machine">Washing Machine</option>
   <option value="Tumble Dryer">Tumble Dryer</option>
    <option value="Dishwasher">Dishwasher</option>
     <option value="Microwave">Microwave</option>
      <option value="Fridge">Fridge</option>
       <option value="Printer">Printer</option>
       <option value="Other">Other</option>
</select>
    </div>
    
      <div class="form-group">
      <label for="repair_description">Repair Description</label>
      <input type="text" name="repair_description" class="form-control" id="repair_description" placeholder="Laptop is dead...">
    </div>
    
      <div class="form-group">
      <label for="hardware_details">Hardware Details</label>
      <input type="text" name="hardware_details" class="form-control" id="hardware_details" placeholder="Black Lenovo Laptop with Charger">
    </div>
    
      <div class="form-group">
      <label for="diagnostic_fee">Diagnostic Fee</label>
      <input type="text" name="diagnostic_fee" class="form-control" id="diagnostic_fee">
    </div>
    
     <div class="form-group">
     <label for="tech_assigned">Technician Assigned</label>

<select name="tech_assigned" id="tech_assigned">
  <option value="Not Assigned Yet">Not Assigned Yet</option>
  <option value="Brendon">Brendon</option>
  <option value="Gabriel">Gabriel</option>
  <option value="Tapiwa">Tapiwa</option>
   <option value="Conrad">Conrad</option>
  </select>
    </div>
    
     <div class="form-group">
     <label for="current_status">Current Status</label>

<select name="current_status" id="current_status">
  <option value="Pending">Pending</option>
  <option value="In Progress">In Progress</option>
  <option value="On Hold Spares Required">On Hold Spares Required</option>
  <option value="On Hold Other Fault">On Hold Other Fault</option>
   <option value="Repair Completed">Repair Completed</option>
  </select>
    </div>
    
      <div class="form-group">
      <label for="technician_notes">Technician Notes</label>
      <input type="text" name="technician_notes" class="form-control" id="technician_notes">
    </div>
    
       <div class="form-group">
      <label for="admin_notes">Admin Notes</label>
      <input type="text" name="admin_notes" class="form-control" id="admin_notes">
          </div>
          
          <div class="form-group">
     <label for="invoice_status">Invoice Status</label>

<select name="invoice_status" id="invoice_status">
  <option value="Client Not Yet Invoiced">Client Not Yet Invoiced</option>
  <option value="Client Invoiced">Client Invoiced</option>
   </select>
    </div>
    
     <div class="form-group">
      <label for="invoice_number">Invoice Number</label>
      <input type="text" name="invoice_number" class="form-control" id="invoice_number">
          </div>
<input type="submit" id="btn_create" name="btn_create" class="btn btn-primary" value="Create Job Card">

    </form>

我的表单操作页面:

<?php

require_once "connection.php";

if(isset($_REQUEST['btn_create']))
{
    $job_number = $_REQUEST['job_number'];
    $date = $_REQUEST['date'];
    $client_full_name = $_REQUEST['client_full_name'];
    $client_email = $_REQUEST['client_email'];
    $client_phone = $_REQUEST['client_phone'];
    $item_for_repair = $_REQUEST['item_for_repair'];
    $repair_description = $_REQUEST['repair_description'];
    $hardware_details = $_REQUEST['hardware_details'];
    $diagnostic_fee = $_REQUEST['diagnostic_fee'];
    $tech_assigned = $_REQUEST['tech_assigned'];
    $current_status = $_REQUEST['current_status'];
    $technician_notes = $_REQUEST['technician_notes'];
    $admin_notes = $_REQUEST['admin_notes'];
    $invoice_status = $_REQUEST['invoice_status'];
    $invoice_number = $_REQUEST['invoice_number'];
  
 if(empty($date)){
  $errorMsg="Please Enter date";
 }
 else if(empty($client_email)){
  $errorMsg="Please Enter Email Address";
 }
 else
 {
  try
  {
   if(!isset($errorMsg))
   {
    $insert_stmt=$db->prepare('INSERT INTO repairs(job_number,date,client_full_name,client_email,client_phone,item_for_repair,repair_description,hardware_details,diagnostic_fee,tech_assigned,current_status,technician_notes,admin_notes,invoice_status,invoice_number) VALUES(:job_number,:date,:client_full_name,:client_email,:client_phone,:item_for_repair,:repair_description,:hardware_details,:diagnostic_fee,:tech_assigned,:current_status,:technician_notes,:admin_notes,:invoice_status,:invoice_number)');      
                $insert_stmt->bindParam(':job_number', $job_number);
                $insert_stmt->bindParam(':date', $date);
                $insert_stmt->bindParam(':client_full_name', $client_full_name);
                $insert_stmt->bindParam(':client_email', $client_email);
                $insert_stmt->bindParam(':client_phone', $client_phone);
                $insert_stmt->bindParam(':item_for_repair', $item_for_repair);
                $insert_stmt->bindParam(':repair_description',$repair_description);
                $insert_stmt->bindParam(':hardware_details', $hardware_details);
                $insert_stmt->bindParam(':diagnostic_fee', $diagnostic_fee);
                $insert_stmt->bindParam(':tech_assigned', $tech_assigned);
                $insert_stmt->bindParam(':current_status', $current_status);
                $insert_stmt->bindParam(':technician_notes', $technician_notes);
                $insert_stmt->bindParam(':admin_notes', $admin_notes);
                $insert_stmt->bindParam(':invoice_status', $invoice_status);
                $insert_stmt->bindParam(':invoice_number', $invoice_number);   
     
    if($insert_stmt->execute())
    {
     $insertMsg="Created Successfully........sending email now"; 
     
    }
   }
  }
  catch(PDOException $e)
  {
   echo $e->getMessage();
  }
 }
}

?>
<?php 
if(isset($_POST['btn_create'])){
    $to = "EMAIL_ADDRESS"; // this is your Email address
    $from = "EMAIL_ADDRESS"; // this is the sender's Email address
    $job_number = $_POST['job_number'];
    $date = $_POST['date'];
    $client_full_name = $_POST['client_full_name'];
    $item_for_repair = $_POST['item_for_repair'];
    $subject = "JC$job_number has been added to ECEMS";
   $message = "Hi Admin. A new job card has been added to ECEMS on the $date for $client_full_name. The item for repair is a $item_for_repair. Please start diagnostics immediately for this item.";
       $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    header("refresh:1;repairs.php"); 
            }
?>

我试着按照这个啧啧:Send email with PHP from html form on submit with the same script

我也尝试激活此页面上的错误但没有结果:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

我是新手,从来没有做过像这样需要在电子邮件中发送 AUTO INCREMENT 号码的代码。请有人帮助我。如果需要更多说明,我可以编辑我的问题。

编辑:我做了一些研究,发现我可以使用 lastInsertID(https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id 函数。一些帮助实现这一点将不胜感激。

 $insertId = false;
 if($insert_stmt->execute())
    {
     $insertId = $insert_stmt->insert_id;      
     $insertMsg="Created Successfully........sending email now"; 

     
    }

if($insertId){

   // do stuff with the insert id
}

好的,所以我使用 MAX 方法解决了这个问题。我在 createnewrepairs.php 页面的顶部添加了以下内容:

$stmt = $db->prepare("SELECT MAX(job_number) AS max_id FROM repairs"); $stmt -> execute(); $job_number = $stmt -> fetch(PDO::FETCH_ASSOC); $max_id = $job_number['max_id'];

然后在同一页面上,我添加了以下表单字段

<div class="form-group">
  <label for="job_number">Job Number</label>
  <input type="job_number" name="job_number" id="job_number" class="form-control" value="<?php echo $max_id+1;?>" readonly>
</div> 

然后在表单处理页面 (processnewrepair.php) 我原来的代码 post 工作并生成了 AUTO INCREMENT NUMBER 以在电子邮件中发送。