Php 使用pdo和mysql插入3个不同的表

Php 使用pdo和mysql插入3个不同的表,php,mysql,Php,Mysql,我使用的是php mysql,我正在尝试插入3个不同的表,但第三个表没有收到值,这是最后一个表。我的想法是在创建用户的新帐户时,将用户id插入所有表中,以便在我要更新其他字段时,我只引用该用户id,它是其他表中的外键桌子。 下面是我的代码: <?php #connect to the db require_once('db.inc.php'); ?> <?php $date_created = date('y-m-d h:i:s a'); $

我使用的是php mysql,我正在尝试插入3个不同的表,但第三个表没有收到值,这是最后一个表。我的想法是在创建用户的新帐户时,将用户id插入所有表中,以便在我要更新其他字段时,我只引用该用户id,它是其他表中的外键桌子。 下面是我的代码:

<?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();
    }
?>


在第三次查询中,当您使用
上次插入\u ID()
时,它将获取在紧接前一次查询中插入的记录的ID,即第二次查询,而不是第一次查询。您需要在第一个查询之后进行单独的查询,以获取
最后一个\u INSERT\u ID()
,将其存储在PHP中的一个变量中,并在其他两个查询中使用它。

您能稍微缩进您的代码吗?无法读取…第2和第3条insert语句的顺序是否相关?在tish_用户表上使用mysql insert触发器如何?太好了,我会立即尝试