0

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']);

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

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.

Community
  • 1
  • 1
user2698144
  • 11
  • 1
  • 5
  • You said you're already using `INSERT`, I'm guessing it's not working for you? Show us your code. – rath Aug 20 '13 at 01:40
  • hey, no i havent inserted the data yet. I want to know how to do it. – user2698144 Aug 20 '13 at 01:47
  • http://dev.mysql.com/doc/refman/5.5/en/insert.html – Connor Peet Aug 20 '13 at 02:11
  • hey, sorry i meant to say i know how to use the insert command I just need help with the below: 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. – user2698144 Aug 20 '13 at 02:22
  • Can you add your insert query that is not working or have you not formed one yet? – skv Aug 20 '13 at 03:08

1 Answers1

0

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].')';

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));
skv
  • 1,793
  • 3
  • 19
  • 27
  • Thanks, I want the campaign ID to be the same for each row. Here is what I Got. The only problem now is that the states are not being added in. My database just has the campaign ID and it says array in the states field. Any idea what I am doing wrong $campaign[counter] = 5; if(isset($_POST['submit_complete'])){ $tags = explode(',', $_SESSION['stateslist']); $sql = array(); $counter=0; foreach( $tags as $row ) { $counter++; $sql[] = '("'.$row.'", '.$campaign[counter].')'; } mysql_query('INSERT INTO campaign_states (state_abbr, campaign_id) VALUES '.implode(',', $sql)); – user2698144 Aug 20 '13 at 17:43
  • $campaign[counter] = 5; if(isset($_POST['submit_complete'])){ $tags = explode(',', $_SESSION['stateslist']); $sql = array(); $counter=0; foreach( $tags as $row ) { $counter++; $sql[] = '("'.$row.'", '.$campaign[counter].')'; } mysql_query('INSERT INTO campaign_states (state_abbr, campaign_id) VALUES '.implode(',', $sql)); – user2698144 Aug 20 '13 at 17:45