-2

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.

rleir
  • 791
  • 1
  • 7
  • 19
user2698144
  • 11
  • 1
  • 5
  • 1
    Kindly do not post the same question several times it will not help you nor will help us. – Prix Aug 20 '13 at 02:30

1 Answers1

0

There are several ways to pass along some variables from one script to another, but if you really want to use session vars for that, I guess you should:

cast your key-value array as an associative array

$myvar=array('campaign_id'=>5, 'states'=>array('NY,CA'));

json_encode it into a string which will fit into your session variable.

  $_SESSION['campaign_state']=json_encode($myvar);

once you retrieve the session var you json_decode it:

 $myarray=json_decode($_SESSION['campaign_state'],true);

iterate over the 'states' key:

$query=array();
foreach($myarray['states'] as $state) {
    $query[]=insert into mytable (campaign,state) values ({$myarray['campaign_id')},$state);
}

It's a dirty cheap solution and you still should sanitize those variables.

ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • thanks, I am going to give it a try. I am only using session variables because I had to use header( "Location: "" ); in my forms and I did not want to pass the information through my URL. – user2698144 Aug 20 '13 at 02:39