在两个表中插入值

时间:2014-03-05 19:20:40

标签: php mysql sql

完成表单后,我正在创建PDF并将表单的一些值存储到表中。我想将表单值存储到单独的表中。我无法将代码存储到两个表中。这是我的工作代码而不添加第二个SQL查询:

<?php
session_start(); 
$errors = array(); 
include('connect.php'); 

//if($_POST['email'] == ''){
//    $errors[]= 'Email cannot be empty';  
//}


if( count($errors) === 0 ) 
{
    $description = pathinfo(__FILE__, PATHINFO_FILENAME);  
    $filename = date('Ymdhis').".pdf"; 
    $username = $_SESSION['username']; 
    $sql = "INSERT INTO documents (id, userid, description, name, date) VALUES (NULL, {$_SESSION['id']}, '{$description}' ,'{$filename}', NOW())"; 
     $success = mysql_query($sql);    

    include('../MPDF/mpdf.php'); 
    ob_start(); 
    include('../documents/siteSubmit.php');
    $html = ob_get_contents(); 
    ob_end_clean(); 
    ob_end_flush(); 

    $mpdf=new mPDF();
    $folder = "../userDocuments/{$_SESSION['username']}"; 
    if(!is_dir($folder))
    {
        mkdir($folder, 0755, true);  
    } 
    $mpdf->writeHTML($html); 
    $mpdf->Output('../userDocuments/'.$_SESSION['username'].'/'.$filename); 
    echo json_encode(array('succes' => true)); 
}
else 
{
    echo json_encode(array('errors' => $errors)); 
}

这是我想要添加到另一个表

的值
$sql = "INSERT INTO sitesubmit (first_name, last_name, email, telephone, truck_number, truck_mileage, carrier, site_number, lat, longitude, comments, job_completion) VALUES ('first_name', 'last_name', 'email', 'telephone', 'truck_number', 'truck_mileage', 'carrier', 'site_number', 'lat', 'longitude ', 'comments')"; 

$success = mysql_query($sql);

1 个答案:

答案 0 :(得分:1)

您可以使用此查询但名称不同。像那样

  $sql = "INSERT INTO documents (id, userid, description, name, date) VALUES (NULL, {$_SESSION['id']}, '{$description}' ,'{$filename}', NOW())"; 
  $success = mysql_query($sql);  
  $sql2 = "INSERT INTO sitesubmit (first_name, last_name, email, telephone, truck_number, truck_mileage, carrier, site_number, lat, longitude, comments, job_completion) VALUES ('first_name', 'last_name', 'email', 'telephone', 'truck_number', 'truck_mileage', 'carrier', 'site_number', 'lat', 'longitude ', 'comments','job_completion')"; 

 $success2 = mysql_query($sql2);  

编辑:

第二个查询缺少job_completion的值,因此此查询将失败。

你必须使该列的值。