0

I created a form with check boxes:

<input type="checkbox" name="cars[]" value="Toyota" /> Toyota
<input type="checkbox" name="cars[]" value="Honda" /> Honda
<input type="checkbox" name="cars[]" value="Nissan" /> Nissan

On the php part, I read the array and insert into it like this:

$carvalue = "";
foreach($_POST['cars'] as $single){
    $carvalue .= $single;
    $carvalue .= ", ";
}

I insert into my mysql db table the $carvalue variable separated values (eg Toyota, Honda, Nissan). Is this correct?

5 Answers 5

2

This might make it more readable, and you'll get rid of the trailing comma:

$carvalue = implode(",", $_POST['cars']);

Of course, first check if $_POST['cars'] exists and is not empty, etc.

However, since you're storing this into the database, you might want to consider using something other than comma separated values, perhaps JSON.

Sign up to request clarification or add additional context in comments.

Comments

0

If your following normal forms,then that would be a bad idea as it will violate the first normal form (Bcoz single field has been allowed to contain multiple values).Otherwise I dont see any thing wrong in that.

Reference for normal form: http://en.wikipedia.org/wiki/First_normal_form

Comments

0

What is correct will depend on your use for the data, but there are some things to watch out for with concatenating these values to a string. For example, if you want to find all rows that have "Honda" ever (and you probably will), this might be a bad way to store the data.

I would probably make some extra tables: car_make, car_make_group, and car_make_to_group . I'd put

car_make (make_id, make_desc)
------------------
1, "Toyota"
2, "Honda"
3, "Nissan"

I would number my groups like a bit flag enum, but that's not really important..it is just a way to assign numbers to groups in a way that doesn't collide. It also has the property that everything is additive (group2 + group4 = group6). I always provide an association table, though.

car_make_group (make_group_id, make_group_desc)
------------------
1, "Toyota"
2, "Honda"
3, "Toyota, Honda"
4, "Nissan"
5, "Toyota, Nissan"
6, "Honda, Nissan"
7, "Toyota, Honda, Nissan"

Finally, I would make an association table to help with queries:

car_makes_to_group (make_id, make_group_id)
------------------
1, 1
1, 3
1, 5
1, 7
2, 2
2, 3
2, 6
2, 7
3, 4
3, 5
3, 6
3, 7    

Using multiple tables will allow you some flexibility for future changes. For example, you could realize later that you want to use JSON and change the make_group_desc to hold the JSON string instead of the comma-separated string.

You'll be able to run an ad-hoc query where you find every row that includes "Toyota" also:

Select m.make_id, m.make_desc, g.make_group_id, g.make_group_desc
FROM car_make_to_group t
INNER JOIN car_make_group g
ON (t.make_group_id = g.make_group_id)
INNER JOIN car_make m
ON (t.make_id = m.make_id)
WHERE m.make_desc = "Toyota" 

Comments

0

Try this:

<?php
$carvalue = '';
if(!empty($_POST['cars']))
{
      $carvalue = implode(",", $_POST['cars']);
      $carvalue = rtrim($carvalue, ",");
}
?>

Use $carvalue to insert values in the DB

Comments

-1

It might be better to use json_encode to create a json string and store that in the database. This would make it easier to turn the data back into a usable format (back into an array) if required later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.