Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Tiffany McAllister
Tiffany McAllister
25,806 Points

Insert comma separated string from form into MySQL database

I'm currently doing an assignment for a movie database. I have the following tables:

MOVIES: Movie_ID, Title, Genre etc.

ACTORS: Actor_ID, First Name, Last Name

(Linking table) MOVIE_ACTORS: ID, Movie_ID, Actor_ID

I need the user to be able to add a movie to the database so I have a form with the following fields: Movie Title, Genre, Actors... in the Actors input field I am asking them to enter multiple actors separated by a comma, but now I'm confused as to how I can actually get the separate first names and last names and insert them into the Actors table. How would I go about doing this, or is there a better way to structure my form?

Any help would be greatly appreciated :)

1 Answer

Dekel Zoaretz
Dekel Zoaretz
14,864 Points

You can use php explode function to split between the names with the delimiter of comma and you will get an array of the names to insert to the database. To know the first and last name of the actors you need two different input fields for the user to type - one for first name and second for last - if you want to be sure for the first and last name. That way you can insert the data exactly the same as the user typed.

Tiffany McAllister
Tiffany McAllister
25,806 Points

I will look into the explode function.. thank you :)

The only problem with having two different inputs for first name and last name is that I want the user to be able to add multiple actors.