【发布时间】:2009-04-30 22:03:49
【问题描述】:
我有以下代码用于将表单数据插入到我的数据库中的单个表中。`
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_users_interests_table
- users_id
- subcategories_id
jss_info_requests_table
- users_id
- provider_id
- subcategories_id
jss_providers_assignments_table
- provider_id
- subcategories_id
因此,在将数据插入jss_users_interests_table 之后,我需要做的是将相同的数据以及每个subcategories_id's 对应的provider_id 从 jss_provider_assignment_table 插入到 jss_info_requests_table。说得通?我是不是把它搞砸了,让它变得复杂?
任何有关语法的帮助都会很棒。谢谢!
【问题讨论】: