Connecting Tech Pros Worldwide Forums | Help | Site Map

Form with checkboxes to mySQL returns as "array".

faugustin's Avatar
Newbie
 
Join Date: Aug 2009
Location: Sweden
Posts: 5
#1: Aug 30 '09
Hi, I have a problem. I have av form with textfields, textareas and a vector type fieldset of checkboxes. Everything but the checkboxes works fine, they result in "array" in the field in the database.
Code:
Expand|Select|Wrap|Line Numbers
  1. "...<fieldset>
  2. <legend>Genre</legend>
  3. <input type="checkbox" name="genre[]" value="drama">&nbsp;Drama
  4. <input type="checkbox" name="genre[]" value="thriller">&nbsp;Thriller
  5. <input type="checkbox" name="genre[]" value="action">&nbsp;Action
  6. <input type="checkbox" name="genre[]" value="dokumentär">&nbsp;Dokumentär...
actionfile:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("localhost","root","");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.  
  8. mysql_select_db("inavutan", $con);
  9.  
  10. $sql="INSERT INTO sedda_filmer (titel, huvudroll, genre, intrig, regi, annat)
  11. VALUES
  12. ('$_POST[titel]','$_POST[huvudroll]','$_POST[genre]','$_POST[intrig]','$_POST[regi]','$_POST[annat]')";
  13.  
  14. if (!mysql_query($sql,$con))
  15.   {
  16.   die('Error: ' . mysql_error());
  17.   }
  18. echo "1 film tillagd";
  19.  
  20. mysql_close($con)
  21. ?>
How do I "convert" the vector/ array "gengre" to result in the checked values?

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Aug 30 '09

re: Form with checkboxes to mySQL returns as "array".


You could implode() them, but it depends on how you want to handle them.

Expand|Select|Wrap|Line Numbers
  1. print implode(", ", $_POST['genre']);
  2.  
faugustin's Avatar
Newbie
 
Join Date: Aug 2009
Location: Sweden
Posts: 5
#3: Aug 30 '09

re: Form with checkboxes to mySQL returns as "array".


Ok, Thanx! I want the result in the field "genre" in "sedda_filmer", as text. Then I present them in html by my "select.php", with chosen fields.
I'll try your suggestion and report back later...
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#4: Aug 30 '09

re: Form with checkboxes to mySQL returns as "array".


Hi.

I assume, by your use of the checkboxes, that you mean each film to have multiple genres?

A single field in a database should never contain more then a single piece of data, meaning that a single "genre" field would not be enough for this information.
You could always cheat on this by serializing all the data and storing it as one string (which is what the implode function does), but that's just not... sanitary ;-)

The "best" solution for something like this is to create a separate table for the genres and link it to the film table.
Expand|Select|Wrap|Line Numbers
  1. +--------+    +------------+
  2. | Film   |    | FilmGenre  |    +---------+ 
  3. +--------+    +------------+    | Genre   |
  4. | FilmID |<-->| FilmID_FK  |    +---------+ 
  5. | Title  |    | GenreID_FK |<-->| GenreID |
  6. | Date   |    +------------+    | Name    |
  7. | Etc... |                      +---------+
  8. +--------+
There you would just add all the Genres to the Genre table, all the Films to the Film table, and then a row in the FilmGenre table for each genre a film should have.

P.S.
You should read up on SQL Injection. You code is wide open to it.
The short version; Don't put anything into a MySQL query before running it through the mysql_real_escape_string function.
faugustin's Avatar
Newbie
 
Join Date: Aug 2009
Location: Sweden
Posts: 5
#5: Aug 31 '09

re: Form with checkboxes to mySQL returns as "array".


Wow, Thank you very much! This sounds like a really sane idéa. I've thought about linking tables earlier but chrashed since I'm new to this. I've tried with different textfields for each genre, witch worked, but the form wasn't so cool. Since I have an Excel sheet with 50 films and fields and being lazy, I've fooled around a lot with the data and got it to HTML via XML...I understand what U're saying, Thanx.
Reply

Tags
checkbox, mysql, php