简体   繁体   English

如何修复将数据插入第二张表

[英]How to fix the inserting data into the second table

I am trying to insert the same data into the second table based on the first table created UserID (auto-increment). 我试图基于创建的第一个表的UserID (自动递增)将相同的数据插入第二个表。

I created an INSERT query which inserts the user registration data into the users_tb table. 我创建了一个INSERT查询,它将用户注册数据插入到users_tb表中。 It works successfully. 它可以成功工作。 Then I need to insert the address describing data into the address_tb table, based on the UserID in users_tb . 然后,我需要根据users_tbUserID将描述数据的地址插入到address_tb表中。 But unfortunately it doesn't work. 但不幸的是,它不起作用。

//Rady to insert:
$results = $Street. ", " .$Apt. ", " .$City. ", " .$State. ", " .$ZipCode. ", " .$Country; 
$query = mysql_query("INSERT INTO users_tb (UserId , FirstName , LastName , Email , Username , Password , Street , Apt , City , State , ZipCode , Country , Address , HomePhone , MobilePhone , Lat, Lng, Membership) VALUES ('' , '$First_Name' , '$Last_Name' , '$Email' , '$Username' , '$hashedPW' , '$Street' , '$Apt' , '$City' , '$State' ,'$ZipCode' ,'$Country' , '$results' , '$Ph_Number' , '$Mb_Number' , '0' , '0' , '1')");

// Insert Data into address_tb
$ad = mysql_query("SELECT UserID , Username FROM users_tb WHERE Username = $Username");
$adad = mysql_fetch_row($ad);
$UserID = mysql_real_escape_string($adad['UserID']);
$query = mysql_query("INSERT INTO address_tb (AddressID , Street , Apt , City , State , ZipCode , Country , Address , Lat , Lng , UserID) VALUES ('' , '$Street' , '$Apt' , '$City' , '$State' ,'$ZipCode' ,'$Country' , '$results' , '0' , '0' , '$UserID')");

I need to have the address fields also in my address_tb table with the respective UserID . 我还需要在具有各自UserID address_tb表中具有地址字段。

$Username should be enclosed in single quotes since it's a string. $Username是字符串,因此应将其括在单引号中。

$ad = mysql_query("SELECT UserID , Username FROM users_tb WHERE Username = '$Username'");

Second problem is, [mysql_fetch_row][1] returns enumerated array ie key as numeric indexes. 第二个问题是, [mysql_fetch_row][1]返回枚举数组,即键作为数字索引。 Change is mysql_fetch_assoc . 更改为mysql_fetch_assoc

$adad = mysql_fetch_assoc($ad);

Another approach is to get last insert id and use it to add address. 另一种方法是获取最后一个插入ID并使用它添加地址。 After inserting into users table, use below method to retrieve last user id 插入用户表后,使用以下方法检索上一个用户ID

$last_user_id = mysql_insert_id();

Use $last_user_id directly into the insert query on address_tb . 直接在address_tb上的插入查询中使用$last_user_id

Please note mysql is deprecated. 请注意,不建议使用mysql You should use mysqli with parameterized queries ( https://www.w3schools.com/php/php_mysql_prepared_statements.asp ). 您应该将mysqli与参数化查询( https://www.w3schools.com/php/php_mysql_prepared_statements.asp )一起使用。

This can be done in 1 query, using INSERT SELECT : 这可以使用INSERT SELECT在1个查询中完成:

INSERT INTO address_tb (AddressID,
                        Street,
                        Apt,
                        City,
                        State,
                        ZipCode,
                        Country,
                        Address,
                        Lat,
                        Lng,
                        UserID)
SELECT (AddressID,
        Street,
        Apt,
        City,
        State,
        ZipCode,
        Country,
        Address,
        Lat,
        Lng,
        UserID)
FROM users_tb
WHERE users_tb.Username = ?;

I did it. 我做的。 Thank you all of you. 谢谢大家 To tell the truth I didn't understand what was the reason that the second INSERT query didn't complete the data in address_tb. 说实话,我不明白第二个INSERT查询没有完成address_tb中数据的原因是什么。 I only took the first query and edited as the second query and it starts working. 我只接受了第一个查询,并将其编辑为第二个查询,它开始工作。 As I see there aren't any differences in my new query, which I got from editing the first one. 如我所见,我的新查询没有任何区别,这是我编辑第一个查询得到的。 Here is my second query: 这是我的第二个查询:

<< $sql2 = mysql_query("INSERT INTO address_tb (AddressID , Street , Apt , City , State , ZipCode , Country , Address , Lat , Lng , UserId) VALUES ('' , '$Street' , '$Apt' , '$City' , '$State' ,'$ZipCode' ,'$Country' , '$results' , '0' , '0' , '$UserID')"); << $ sql2 = mysql_query(“ INSERT INTO address_tb(AddressID,Street,Apt,City,State,ZipCode,Country,Address,Lat,Lng,UserId)VALUES(”,'$ Street','$ Apt',' $ City','$ State','$ ZipCode','$ Country','$ results','0','0','$ UserID')“); >> >>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM