I’m using the following code to grab my form’s data and send to MYSQL:
$id=$_POST['id'];
$tag=$_POST['tag'];
$race=$_POST['race'];
$city=$_POST['city'];
$pic=($_FILES['photo']['name']);
and inserting it into my table using:
mysql_query("INSERT INTO `people` VALUES ('$id', '$tag', '$race', '$city', '$pic')") ;
My problem is that I need to allow users to enter multiple values (separated by commas or spaces) into the ‘tag’ field of my form to allow for multiple people to be tagged in the same image.
So I have 2 questions. First, is it best practice to create entirely new database entries for each ‘bib’ tag? Or would it be better to store the comma delimited list in a single column?
Secondly, I have been advised to use the explode function. However, I haven’t been able to figure out how to translate this into my current INSERT query. If anyone could steer me in the right direction, I’d really appreciate it. Very new to PHP and just trying to teach myself some basics.
Thanks.
So I have 2 questions. First, is it best practice to create entirely new database entries for each ‘bib’ tag? Or would it be better to store the comma delimited list in a single column?
Storing a comma-separated list in a database column is generally a mistake. It will make it very hard to write queries that try to find out (for example) which images a given person has been tagged in. You should read up on database normalisation. Ordinarily what you will do in a case like this, where a given photo can contain arbitrarily many people, is have a table
image_personwith two columns: one column specifies the image ID, and the other column specifies a person. A row in the table corresponds to the statement “this person has been tagged in this image”. This makes it much easier to query the information you have stored.Secondly, I have been advised to use the explode function.
Assuming that you do not go for the comma-separated list approach (as I would recommend), the only place you might need to use explode in this code is if you pass the list of people to be tagged as a comma-separated list in one of the POST variables. In that case you might do something like:
You could then take either of two approaches:
Additional comment
Please read about SQL injection and make sure that you write secure code. The code you posted in your question is highly insecure.