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

Posted

技术标签:

【中文标题】使用 PDO/MySQL 将自动增量编号附加到电子邮件中发送和发送电子邮件【英文标题】:Sending and email with the auto Increment number attached to the email using PDO/MySQL 【发布时间】:2021-08-31 03:36:12 【问题描述】:

你好,我最喜欢的人!

我正在尝试在提交表单后发送一封电子邮件,并在电子邮件中附加自动递增编号,因为自动递增编号是客户的工作卡参考编号。到目前为止,我已经成功创建了插入脚本,它将数据完美地插入到数据库中,并且也发送了电子邮件。但不会将 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"); 
            
?>

我试图遵循这个 tut: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);

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

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

【问题讨论】:

这个语句中jobnumber从哪里来,$job_number = $_REQUEST['job_number']; jobnumber 应该是查询的结果,比如 $jobId = $insert_stmt->insert_id; $job_number 是表单提交时插入 MySQL 的 AUTO INCREMENT 值。我无法将 job_number 添加到我的 HTML 表单,因为它不是最终用户输入表单的值,所以我知道我的 $job_number = $_REQUEST['job_number'];在我的表单操作页面上是没用的。你能给我一个例子来说明你的 $jobId = $insert_stmt->insert_id; 是什么意思吗?请回答 【参考方案1】:
 $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

【讨论】:

我在这里听起来像个彻头彻尾的白痴,但我应该把它放在哪里,我应该放什么 // 用插入 ID 做一些事情。您能否分享一个链接,让我可以研究/重新搜索您的建议,以便我了解更多信息? 我可以把 return $job_number;? 当然,将 insertId 替换为 job_number。在 if 语句中,您可以发送邮件。【参考方案2】:

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

$stmt = $db-&gt;prepare("SELECT MAX(job_number) AS max_id FROM repairs"); $stmt -&gt; execute(); $job_number = $stmt -&gt; 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) 上,我的原始帖子中的代码运行并生成了自动增量编号以通过电子邮件发送。

【讨论】:

以上是关于使用 PDO/MySQL 将自动增量编号附加到电子邮件中发送和发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

mysql:插入数据和自动增量

Sqoop 增量导入“无法将文件附加到目标目录”

如何在猫鼬中插入自动增量编号

将文件附加到电子邮件时,是不是需要先将其存储在服务器上?

如何在失控的表格中停止自动编号?

即使使用事务回滚,SQL标识(自动编号)也会增加