php - how to Insert multiple rows from array saved in a session into table? -
this question exact duplicate of:
i'm passing multiple state values stored in session variable, mysql table via php using 1 insert command , i'm wondering if possible insert each state value different row. have id saved in variable insert each state.
$campaign_id each state stored in session variable.
print_r($_session['stateslist']); assuming have 2 states saved in $_session['stateslist'] (ny, ca) , campaign id 5, database this
campaign_id state 5 ny 5 ca
i know similar insert multiple rows via php array mysql having problems inserting each state saved in session campaign_id.
there several ways pass along variables 1 script another, if want use session vars that, guess should:
cast key-value array associative array
$myvar=array('campaign_id'=>5, 'states'=>array('ny,ca'));
json_encode string fit session variable.
$_session['campaign_state']=json_encode($myvar);
once retrieve session var json_decode it:
$myarray=json_decode($_session['campaign_state'],true);
iterate on 'states' key:
$query=array(); foreach($myarray['states'] $state) { $query[]=insert mytable (campaign,state) values ({$myarray['campaign_id')},$state); }
it's dirty cheap solution , still should sanitize variables.
Comments
Post a Comment