Log in

View Full Version : Submitting POST array to MySQL?



Cronos
05-08-2008, 09:22 AM
I have a quite messy POST array (coming from a FORM) I want to nicely submit to a DB.

The array could at best look like this (although the order can be different, and the numbering is not necessarily consistent)

[title0] => Iron Man
[year1] => 2008
[country2] => US
[director3] => Jon Favreau
[genre4] => Sci-Fi
[genre5] => Action
[genre6] => Adventure
[actor7] => Robert Downey Jr.
[actor8] => Jeff Bridges
[actor9] => Gwyneth Paltrow
[actor10] => Terrence Howard
[runningtime11] => 126

I want to put this into a DB that looks like this (I think)
Movie

movie_id
title
year
runningtime

Persons

person_id
name

Country

country_id
country_name

Actor_map

movie_id
person_id

Director_map

movie_id
person_id

Country_map

movie_id
country_id



And I want to account for possible existing values, ie. if person "Robert Downey Jr." already exists (either as Director or Actor), don't insert this as a new value, only update the links.

My first idea is as follows:

Parse the array element by element
Make a Switch/Case and sort the elements belonging together into a new array (one array per 'category')
(Somehow) add the new arrays to the DB

However, I feel that this is not very efficient, and I also have a little trouble entering the info into the database (bearing in mind not to duplicate values).

Any help?

boogyman
05-08-2008, 01:46 PM
I would use a server-side language such as php to process the information, before you submit / update the DB

Cronos
05-09-2008, 07:59 AM
Well, that's why I posted this in the PHP forum. :P
I need some help with the code...

boogyman
05-09-2008, 01:16 PM
i am not sure what the numerical index is next to the values you have in the example, but first you would need to gather all of your values, from the form... then you would split out the movie information from the actor information.
then you would check if the movie existed. if it does update the values, if it doesn't then insert it into the database
then you would loop through the actors, see if they exists, if they do great, if not insert it.
then you would do 1 final step by grabbing the actor id and the movie id and insert it into the reference table