php - Inserting array values into SQL -


i randomized set of questions using following code:

for($i=0; $i < count($nwi); $i++)  $itemorder[$i] = $i; shuffle($itemorder);  $_session["itemorder"] = $itemorder; 

a few pages later, portion of questions presented:

for ($i=0; $i<40; $i++) {         if ($i % 10 ==0) echo $header;         echo '<tr class="';          if(in_array($itemlabel[$_session["itemorder"][$i]], $errors)) echo 'skip';          elseif ($i % 2 == 0) echo 'even';                          else echo 'odd';                                          echo '">';         echo '<td>' . ($i + 1) . ".</td><td>" . $itemtext[$_session["itemorder"][$i]] . '</td>';         ($j = 1; $j <= 6; $j++) {             echo "\n" . '<td';             if ($j == 6) echo ' style="background-color:#999;"';             echo '><input type="radio" name="';             echo $itemlabel[$_session["itemorder"][$i]];             echo '" value="';             echo $j; //value of input             echo '"';             if ($_post[$itemlabel[$_session["itemorder"][$i]]] == $j) echo " checked";             echo '></td>';         } 

at end of survey, trying put answers questions (which should range in value 1-8) sql database:

"insert surveydata      (id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....  values      ('$_session[id]', '$_session[itemorder][0]',   '$_session[itemorder][1]', '$_session[itemorder][2]',   '$_session[itemorder][3]',... 

i getting zeros in sql database regardless of how answer questions. suggestions?

well, first don't see assigning session values, issue code in pattern: '$_session[itemorder][1]'. first, make sure mysql expecting varchar there , not int. if int, form make sure isn't quoted.

more importantly, though, when have associative array in php, need make sure php expects that.

this

$a = array("hi"=>array("world"=>0)); echo "$a[hi][world]"; 

outputs

array[world]

put braces around lookup make sure knows treat array, , put quotes around string indexes:

// note braces , quotes $a = array("hi"=>array("world"=>"here")); echo "{$a["hi"]["world"]}";  

outputs

"here"

but, wonder if wouldn't better off using implode:

$columns = implode(',', $_session['itemorder']); $query   = "insert surveydata                  (id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....             values ('{$_session["id"]}', $columns )"; 

i feel obliged point out that system not seem scaleable, , column names agree_pppg_02 not descriptive. may want go codereview stackexchange site see if can't offer tips on database design.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -