doujiu3768 2016-01-20 16:23
浏览 77
已采纳

将来自2个不同源的数据插入MySQL中的一个表中

I'm very new to MySQL and PHP. I have two tables. The first one is users which has the information for the site users and the second table is called SendRequests.

I want to store the data each user posts (it's like an ad posting site) in the second table where I want the first column to be for the email of the logged-in user so I can find the posts from that user later. This is how I am trying to do it:

Using two INSERTs as follows:

$query1=" INSERT INTO SendRequests (email) SELECT email FROM users
WHERE id = '".$_SESSION['id']."' LIMIT 1";

mysqli_query($link, $query1);

$_SESSION['id']= mysqli_insert_id($link);


$query = "INSERT INTO `SendRequests` (`country`, `city`, `fee`) VALUES
('".mysqli_real_escape_string($link,$_POST['departurecountry'])."',
'".mysqli_real_escape_string($link, $_POST['departurecity'])."',
'".mysqli_real_escape_string($link,$_POST['fee'])."')."')
WHERE id = '".$_SESSION['id']."'";

mysqli_query($link, $query);
  • 写回答

2条回答 默认 最新

  • duan00529 2016-01-20 16:40
    关注

    You can use this query to insert data while searching for your email :

    INSERT INTO SendRequests(email, country, city, fee)
    SELECT email, "FR", "Paris", 56.65
    FROM SendRequests
    WHERE id = 1;
    

    This will works if a row with id 1 is already inserted. You can by this way merge your 2 insert in a single one.

    So if the SendRequests with id 1 is equal to anon@host.com, and you run the query above, you'll have at least 2 rows :

    ID EMAIL          COUNTRY  CITY    FEE
    ========================================
    1  anon@host.com  US       Florida 150.25
    2  anon@host.com  FR       Paris   56.65
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改