Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将类似的数据插入到两个表中_Php_Mysql_Select_Insert - Fatal编程技术网

使用php将类似的数据插入到两个表中

使用php将类似的数据插入到两个表中,php,mysql,select,insert,Php,Mysql,Select,Insert,我使用以下代码将表单数据插入数据库中的单个表中` function insert_interests($uid, $interests) { /* first, we'll delete any entries this user already has in the table */ purge_lookup("jss_users_interests_table", $uid); /* now create the sql insert query */ global $db;

我使用以下代码将表单数据插入数据库中的单个表中`

function insert_interests($uid, $interests) {
/* first, we'll delete any entries this user already has in the table */
    purge_lookup("jss_users_interests_table", $uid);
/* now create the sql insert query */
    global $db;
    $db->query(create_checkbox_query($interests, "jss_users_interests_table", $uid));
}
/* helper function for insert_interests(). removes all rows in $table with $uid */
function purge_lookup($table, $uid) {
    global $db;
    $db->query("DELETE FROM $table WHERE users_id = '".$db->escape($uid)."'");
}
/* helper function for insert_interests(). generates the actual SQL query */
function create_checkbox_query($arr, $table, $uid) {
    $q = "INSERT INTO $table (users_id, subcategories_id) VALUES";
    foreach ($arr as $check) {
    $q .=  " ( '$uid' , $check )" . ",";
    }
/* remove the last comma and return */  
return substr($q, 0, -1);
}`
在这段代码之后,我想使用相同的表单数据与另一个表中的其他数据配对,将新记录插入到另一个表中。这是两个表的结构

jss用户兴趣表

  • 用户识别码
  • 子类别\u id
jss\u信息请求表

  • 用户识别码
  • 提供者id
  • 子类别\u id
jss\u提供者\u分配表

  • 提供者id
  • 子类别\u id
因此,在将数据插入
jss\u users\u interests\u table
之后,我需要做的是将相同的数据以及
子类别\u id
对应的
provider\u id
jss\u provider\u assignment\u table插入jss\u info\u requests\u table\u table。有道理?我是不是把事情搞砸了,把事情搞复杂了

任何语法方面的帮助都会很棒。谢谢

此SQL可能会工作

 INSERT INTO jss_info_requests_table 
 (users_id, provider_id, subcategories_id)
SELECT a.users_id,
 b.provider_id, b.subcategories_id FROM
 jss_users_interests_table a,
 jss_providers_assignments_table b 
 WHERE a.subcategories_id =
 b.subcategories_id AND a.users_id =
 [USERID]

我喜欢咖啡因,你很乐意帮我回答这个问题。它不仅成功了(在修复了我的一个小错误之后),我现在也理解了它的语法。谢谢你抽出时间,祝你一切顺利。