473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Form with checkboxes to mySQL returns as "array".

faugustin
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?
Aug 30 '09 #1
4 4038
Markus
6,050 Expert 4TB
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.  
Aug 30 '09 #2
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...
Aug 30 '09 #3
Atli
5,058 Expert 4TB
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.
Aug 30 '09 #4
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.
Aug 31 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or,...
2
by: Vic Spainhower | last post by:
Hello, I am new to php and MySQL and I'm attempting to run a select query on a MySQL database which is working fine except prior to displaying the table with the results from the select query it...
6
by: sonic | last post by:
what exactly is the difference ? I think that is more suitable for declarative arrays, but not exactly sure why. TIA
4
by: Greg Stark | last post by:
I find myself wishing I had a syntax "LIKE ANY (array)". I don't see much value in the = ANY, = ALL, <> ANY, <> ALL syntax since they're equivalent more or less to IN and NOT IN. But it could be...
14
by: Alex | last post by:
I saw the topic of "wired code " about "point to array" and know a little about it. But I am still confused about the question below: I define a point to array "b" int (*b); then I locate the...
9
by: Frederick Gotham | last post by:
What are your thoughts on the following code? typedef int Coords; int main() { Coords *const p = new Coords; delete p; }
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
0
by: =?Utf-8?B?Q3ltZXI=?= | last post by:
When the datagrid has multi rows and the vertical scrollbar hasn't appeared, if I click the column near the bound or drag horizontal scrollbar, this exception will be throwed. The trace stack as...
4
by: Gilles Ganault | last post by:
Hello I'm puzzled as to why PHP (5) prints this (under FreeBSD 6.3): ========= #!/usr/local/bin/php <?php $dbh = new PDO("sqlite:test.sqlite"); $sql = "CREATE TABLE IF NOT EXISTS calls...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.