Question : mysql php array insert

Hi guys,

im working on a web based app, and i need a bit of help.  i have a a page which has loads of checkboxes it uses the following code and works fine

echo "<form id=\"enterForm\" name=\"dateEnter\" method=\"post\" action=\"test2.php\">";      
$qry = mysql_query("select * from _worktype");
while($row = mysql_fetch_array($qry)){
echo "<label>";
echo "<input type='checkbox' id='$row[id]' name='language[]' value='$row[id]'/>";
echo "$row[area]";
echo "</label><br>";

}echo "<input name=\"send\" type=\"submit\" id=\"send\" value=\"Send!\">";
echo "</form>";

the test2.php file also works but i'd like to change its functionality slightly
//test2.php
if(isset($_POST['language']))
{
   $language = $_POST['language'];
   $n        = count($language);
   $i        = 0;

   while ($i < $n)
   {
      echo "{$language[$i]} <br>";
      $i++;
       // echo serialize($language[$i]);
   }
  }

at the moment it displays the results from the selected checkboxes.  what i would like is to input these results into a mysql field.  i was thinking about the serialize function but im not really sure how to use it.  im kind of new to php, and still learning.

any help would be very much appreciated

Answer : mysql php array insert

OK, I can give you the general design pattern here.

For generating the language checkboxes on the page, you want to have a table that lists all the languages.  You can get the ISO standard list here.  It is a big list.  Keep what you need.  You might have two columns in the list - the ISO639-2 code in one column and the English language name in the other column.
http://www.loc.gov/standards/iso639-2/php/code_list.php

To generate the checkboxes, you SELECT * from the languages table and iterate over that results set.  Use the ISO639-2 code for the value of the checkbox and language[] for the name of the checkbox.

When the form is submitted, the only checkboxes that are present in the $_POST array are the ones that are checked.  They will be in the languages sub-array of the $_POST array.

So in your action script, you would iterate over the POST languages sub-array and do INSERT statements into the pivot table.

To sum up, there are three tables for this part of the app - `languages`, `pivot`, and `persons`  You would index languages on the ISO639 code, persons on the auto_increment id, and pivot on both the language code and the persons id.
Random Solutions  
 
programming4us programming4us