插入来自数组多行保存在一个会话到表? [英] Insert multiple rows from array saved in a session into table?
问题描述
我通过PHP使用一个INSERT命令传递存储在一个会话变量多状态值,到MySQL表,我想知道,如果可能的每个状态值插入到不同的行。我也有保存在一个变量的ID,我想每个国家接入。
I'm passing multiple state values that are stored in a session variable, into a MySQL table via php using one insert command and I'm wondering if its possible to insert each state value into a different row. I also have an ID saved in a variable that I would like to insert with each state.
$campaign_id
每个状态存储在该会话变量。
Each state is stored in this session variable.
print_r($_SESSION['stateslist']);
假设我有保存在两种状态下的 $ _ SESSION ['stateslist']
(纽约州,加利福尼亚州)和竞选ID 5,我想我的数据库看起来像这样
Assuming I have two states saved in the $_SESSION['stateslist']
(NY, CA) and campaign ID 5, I would like my database to look like this
campaign_id state
5 NY
5 CA
我知道这是类似于此<一个东西href=\"http://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql\">insert通过PHP数组多行到mysql 但我有插入保存在会话还有CAMPAIGN_ID每个状态的问题。
I know it is something similar to this insert multiple rows via a php array into mysql but I am having problems inserting each state saved in the session as well as the campaign_id.
推荐答案
下面是我通过了你这个问题的答案要什么版本
Here is my adopted version of what you want from that answer
$tags = explode(',', $_SESSION['stateslist']);
$sql = array();
$counter=0;
foreach( $tags as $row ) {
$counter++;
$sql[] = '("'.$row.'", '.$campaign[counter].')';
这里假设你有一个包含所有相应的广告系列ID竞选阵列
Assuming here that you have a campaign array that contains all the corresponding campaign ids
}
mysql_query('INSERT INTO table (state, campaign_id) VALUES '.implode(',', $sql));
这篇关于插入来自数组多行保存在一个会话到表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!