doubingling4706 2019-02-20 00:42
浏览 244

如何通过id将数据插入另一个表

I want to complete user sign up. firstly user will write username and password, table called users, I have id as primary key. secondly user will write address, phone number and zipcode, table called userdata, I have userid as index(key). I have did a relation between id from table user and userid from table userdata.

So now I want to insert data from php code I did two forms one for user and it has two input for username and password it works well and data inserted.

in the second form I have select option has id from table users and it works well. then in the same form I have three inputs phone number, address and zipcode. so the question is how I can insert the data to userdata by the same to the same id. so userid in table user will be the same id in table userdata.

I need sql code.
I used that : "INSERT INTO userdata(address, phone, zipcode) VALUE (:address, :phone, :zip) SELECT 'id' FROM 'users';"

  • 写回答

1条回答 默认 最新

  • doutan3371 2019-02-20 01:27
    关注

    First :

    $q = "NSERT INTO users(username, password) VALUE ('Adam', '12345678')";
    $r = $connect->query($q);
    
    //get the last id u inserted 
    $last_id =  mysqli_insert_id($connect);
    

    Second :

      $q = "NSERT INTO userdata(address, phone, zipcode,user_id) VALUE ('California', '12345678', '1111','$last_id')";
        $r = $connect->query($q);
    

    and if you want to make the (id) not the (userid) the same just :

    $q = "NSERT INTO userdata(id, address, phone, zipcode) VALUE ('$last_id','California', '12345678', '1111')";
            $r = $connect->query($q);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?