2013-02-04 28 views
-1

我使用PHP的MySQL,我試圖插入3個不同的表,但第三個表沒有收到值,這是最後一個表.The想法是我想要得到的在創建用戶的新帳戶時插入到所有表中的user_id,以便當我想更新其他字段時,我將引用那個user_id,這是其他表中的外鍵。 貝婁是我的代碼:插入到3個不同的表使用PDO和mysql

<?php 
    #connect to the db 
    require_once('db.inc.php'); 
?> 

<?php 
    $date_created = date('y-m-d h:i:s a'); 
    $username = (isset($_POST['username'])) ? trim($_POST['username']): ''; 
    $Previllage =(isset($_POST['Previllage']))? trim($_POST['Previllage']): ''; 

    #second tanble values 
    $title=(isset($_POST['title']))? trim($_POST['title']): ''; 
    $firstname=(isset($_POST['firstname']))? trim($_POST['firstname']): ''; 
    $lastname=(isset($_POST['lastname']))? trim($_POST['lastname']): ''; 
    $client_code=(isset($_POST['client_code']))? trim($_POST['client_code']): ''; 
    $job_approval=(isset($_POST['job_approval']))? trim($_POST['job_approval']): ''; 
    $address=(isset($_POST['address']))? trim($_POST['address']): ''; 
    $cell=(isset($_POST['cell']))? trim($_POST['cell']): ''; 
    $tel=(isset($_POST['tel']))? trim($_POST['tel']): ''; 
    $email=(isset($_POST['email']))? trim($_POST['email']): ''; 
    $company=(isset($_POST['company']))? trim($_POST['company']): ''; 
    $province=(isset($_POST['province']))? trim($_POST['province']): ''; 
    $ip_address=$SERVER['REMOTE_ADDR']; 

    if(empty($_POST['firstname'])){ 
     exit(); 
    } 

    #check box 
    if(isset($_POST['department_type'])) { 
     $value = implode(",", $_POST['department_type']); 
    } else { 
     $value = ""; 
    } 
    #check box 

    #Image code 
    $target ='../t/images/'; 
    $target = $target.basename($_FILES['imagename']['name']); 
    $pic = ($_FILES['imagename']['name']); 
    ######################################################## 
    #end 

    try { 
     $query="INSERT INTO tish_user(username,Previllage,date_created) 
       VALUES(:username,:Previllage,:date_created)"; 
     $insert = $con->prepare($query); 
     $insert->execute(array(
        ':username'=>$username, 
        ':Previllage'=>$Previllage, 
        ':date_created'=>$date_created) 
       ); 

     # insert into another table 
     $query="INSERT INTO tish_clientinfor(
       user_id,title,firstname,lastname, 
       client_code,department_type,job_approval,province, 
       company,address,cell,tel,email,date_registered) 
       VALUES(
       LAST_INSERT_ID(), 
       :title,:firstname,:lastname, 
       :client_code,:department_type,:job_approval,:province,:company,:address, 
       :cell,:tel,:email, 
       :date_registered)"; 

     $insert = $con->prepare($query); 
     $insert->execute(array(
        ':title'=>$title, 
        ':firstname'=>$firstname, 
        ':lastname'=>$lastname, 
        ':client_code'=>$client_code, 
        ':department_type'=>$value, 
        ':job_approval'=>$job_approval, 
        ':province'=>$province, 
        ':company'=>$company, 
        ':address'=>$address, 
        ':cell'=>$cell, 
        ':tel'=>$tel, 
        ':email'=>$email, 
        ':date_registered'=>$date_created) 
       ); 

     #intert into the security table 
     $query = "INSERT INTO tish_security(ip_address,user_id,date_registered) 
       VALUES(
       :ip_address, 
       LAST_INSERT_ID(), 
       :date_registered)"; 
     $insert = $con->prepare($query); 
     $insert->execute(array(
        ':ip_address'=>$ip_address, 
        ':date_registered'=>$date_created) 
       ); 

     # insert into another table 
    } catch(PDOException $e) { 
     echo $e->getMessage(); 
    } 
?> 
+3

你可以縮進你的代碼嗎?這是不可讀的... – j0k

+0

第2次和第3次插入語句的順序是相關的?如何在tish_user表上使用mysql插入觸發器? –

回答

1

在你的第三個查詢,當您使用LAST_INSERT_ID()是越來越這是之前插入查詢記錄,即第二個查詢,而不是第一個的ID。您需要在第一個查詢後進行單獨查詢,以獲得LAST_INSERT_ID(),將其存儲在PHP中的變量中,並將其用於其他查詢中。

+0

偉大我會立即嘗試這個 – humphrey