使用3个表插入INTO

  $ sql3 =“INSERT INTO users_addresses(ua_user_id,ua_address_id)VALUES('','')”  ; 
</ code> </ pre>

我是php的新手,我的提示是将另外一个名为users_addresses的2个表id连接起来。当用户在我的数据库中注册时,我希望 要在users_addresses中克隆的user_id和address_id(ua_user_id,ua_address_id)</ p>

我的表格 </ p>

</ p>

  $ sql =“INSERT INTO 用户(user_fname,user_mname,user_lname,user_login,user_email,user_phone)
VALUES('{$ _SESSION ['userinfo'] ['fname']}','{$ _SESSION ['userinfo'] ['mname']} ','{$ _SESSION ['userinfo'] ['lname']}','{$ _SESSION ['userinfo'] ['login']}','{$ _SESSION ['userinfo'] ['email'] }','{$ _SESSION ['userinfo'] ['phone']}')“;

$ sql1 =“INSERT INTO地址(address_line_1,address_line_2,address_zip,address_city,address_province,address_country)
VALUES('{$ _SESSION ['addressinfo'] ['adr1']}','{$ _SESSION ['addressinfo ''['adr2']}','{$ _SESSION ['addressinfo'] ['zip']}','{$ _SESSION ['addressinfo'] ['city']}','{$ _SESSION [' addressinfo'] ['provinciq']}','{$ _SESSION ['addressinfo'] ['durjava']}')“;

$ sql2 =“INSERT INTO notes(note_text)
VALUES('{$ _SESSION ['noteinfo'] ['note']}')”;
</ code> </ pre>

< p>这些是我用于在DB中添加会话数据的其他SQL代码。</ p>
</ div>

展开原文

原文

 $sql3 = "INSERT INTO users_addresses (ua_user_id,ua_address_id) VALUES ('','')";

I am new in php and my hint is to link 2 tables id's in in another one called users_addresses.When a user is registered in my database i want the user_id and address_id to clone in users_addresses(ua_user_id,ua_address_id)

My tables

enter image description here

$sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) 
            VALUES ('{$_SESSION['userinfo']['fname']}', '{$_SESSION['userinfo']['mname']}', '{$_SESSION['userinfo']['lname']}', '{$_SESSION['userinfo']['login']}', '{$_SESSION['userinfo']['email']}', '{$_SESSION['userinfo']['phone']}')";
    $sql1 = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country) 
            VALUES ('{$_SESSION['addressinfo']['adr1']}', '{$_SESSION['addressinfo']['adr2']}', '{$_SESSION['addressinfo']['zip']}', '{$_SESSION['addressinfo']['city']}', '{$_SESSION['addressinfo']['provinciq']}', '{$_SESSION['addressinfo']['durjava']}')"; 
    $sql2 = "INSERT INTO notes (note_text) 
            VALUES ('{$_SESSION['noteinfo']['note']}')";

These are my others SQL codes for adding session's data in DB.

doulongdan2264
doulongdan2264 使用LEFTJOIN可能重复INSERT
2 年多之前 回复
dongqing4774
dongqing4774 如果您添加更多PHP代码,我们可以帮助您提供更好的示例。
2 年多之前 回复
dpo15099
dpo15099 是的,只是没有人按我想要的方式帮助我,我想再用SQL再问一次,因为否则会让其他人明显感到困惑
2 年多之前 回复
dongsu1951
dongsu1951 看起来很熟悉...stackoverflow.com/questions/50945805/insert-with-left-join
2 年多之前 回复

2个回答

Use mysqli_insert_id() to get the unique ID of the insert table, this example uses Procedural style:

<?php
include 'connection.php';
......
$InsertSQL =    "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) 
                VALUES ('{$_SESSION['userinfo']['fname']}',
                        '{$_SESSION['userinfo']['mname']}',
                        '{$_SESSION['userinfo']['lname']}',
                        '{$_SESSION['userinfo']['login']}',
                        '{$_SESSION['userinfo']['email']}',
                        '{$_SESSION['userinfo']['phone']}')";
$ResultSQL = mysqli_query($conn, $InsertSQL) or die(mysqli_error($conn)); // <-- execute your query
$UserID = mysqli_insert_id($conn); // <-- get the UserID

$InsertSQL =    "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country) 
                VALUES ('{$_SESSION['addressinfo']['adr1']}',
                        '{$_SESSION['addressinfo']['adr2']}',
                        '{$_SESSION['addressinfo']['zip']}',
                        '{$_SESSION['addressinfo']['city']}',
                        '{$_SESSION['addressinfo']['provinciq']}',
                        '{$_SESSION['addressinfo']['durjava']}')"; 
$ResultSQL = mysqli_query($conn, $InsertSQL) or die(mysqli_error($conn)); // <-- execute your query
$AddressID = mysqli_insert_id($conn); // <-- get the AddressID

$InsertSQL =    "INSERT INTO user_addresses (ua_user_id,ua_address_id) 
                VALUES ($UserID,$AddressID)"; // <-- INSERT INTO user_address
$ResultSQL = mysqli_query($conn, $InsertSQL) or die(mysqli_error($conn)); // <-- execute your query
?>

You should also look into SQL Injection vulnerability, check out prepared statements.

Hope that helps.



只需要从第一个sql获取user_id。 如果您正在使用mysqli函数,请执行此操作</ p>

  //运行您的第一个sql:insert user 
mysqli_query($ con,$ sql);
$ user_id = mysqli_insert_id($ CON); //或mysqli :: $ insert_id
</ code> </ pre>

接下来,你有$ user_id变量和用户ID。</ p>

   $ sql1 =“INSERT INTO地址(address_line_1,address_line_2,address_zip,address_city,address_province,address_country)
VALUES($'{$ _ SESSION ['addressinfo'] ['adr1']}','{$ _SESSION ['addressinfo' ] ['adr2']}','{$ _SESSION ['addressinfo'] ['zip']}','{$ _SESSION ['addressinfo'] ['city']}','{$ _SESSION ['addressinfo ''['provinciq']}','{$ _SESSION ['addressinfo'] ['durjava']}')“;

mysqli_query($ con,$ sql);
$ address_id = mysqli_insert_id($ con); //或mysqli :: $ insert_id

$ sql3 =“INSERT INTO users_addresses(ua_user_id,ua_address_id)VALUES($ user_id,$ address_id)”;
mysqli_query($ con,$ sql);
</ code> </ pre>
</ div>

展开原文

原文

Just need get user_id from first sql. If you are using mysqli function, do this

// run your first sql: insert user
mysqli_query($con, $sql);
$user_id = mysqli_insert_id($con); // or mysqli::$insert_id 

Next, you have $user_id variable with user id.

$sql1 = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country) 
        VALUES ($'{$_SESSION['addressinfo']['adr1']}', '{$_SESSION['addressinfo']['adr2']}', '{$_SESSION['addressinfo']['zip']}', '{$_SESSION['addressinfo']['city']}', '{$_SESSION['addressinfo']['provinciq']}', '{$_SESSION['addressinfo']['durjava']}')"; 
mysqli_query($con, $sql);
$address_id = mysqli_insert_id($con); // or mysqli::$insert_id 

$sql3 = "INSERT INTO users_addresses (ua_user_id, ua_address_id) VALUES ($user_id, $address_id)";
mysqli_query($con, $sql);

dtlrp119999
dtlrp119999 示例已更新。 1-插入用户并获取ID。 2-插入地址并获取id。 3-使用user_id和address_id插入users_addresses。
2 年多之前 回复
douhuan2101
douhuan2101 首先添加用户,然后添加地址,最后我想在每次添加数据后在users_addresses表中同时使用两个id。
2 年多之前 回复
dongyo1959
dongyo1959 好的,我修复了这个例子。 然后你插入用户和地址,然后你需要在users_addresses上插入两个id,对吧?
2 年多之前 回复
douchun5969
douchun5969 是的,我想在users_addresses中使用它们
2 年多之前 回复
doue2666
doue2666 因为简单的复制和粘贴,这个答案是不正确的。 @pablorsk更改了您的INSERT语句。
2 年多之前 回复
dongshan3759
dongshan3759 我认为$ sql2语句也不正确。 OP希望在users_addresses表中使用$ user_id和$ address_id,而不是注释。
2 年多之前 回复
dongqie8661
dongqie8661 谢谢@JustBaron,示例已更新。
2 年多之前 回复
douwu8524
douwu8524 你需要数据库连接.. $ user_id = mysqli_insert_id($ conn);
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐