You need to name your text boxes properly using brackets []
to form an array, then loop through your POST'ed array to insert the data. Be certain you're escaping data before running your query by using mysql_real_escape_string
or prepared queries.
EDIT: Updating examples because OP appended information to the question.
In the extended example you've provided, it looks like the taste
only has a few choices. In that case, I would use a <select>
element over a text box. Check out my examples for how, personally, I would do it.
Form
Notable changes: Using a simple loop in PHP, you can specify the number of fields you want to show and easily add other options to the taste select box. I'm placing an arbitrary number within the []
to be certain we link the fruit to the taste when we process the form.
<?php
$n = 5; //Number of fields to show
$tastes = array('Great', 'Good', 'Okay', 'Bad', 'Horrible');
for($i=0;$i<$n;$i++) {
echo '<p>';
echo '<input type="text" name="fruit['.$i.']" /> ';
echo '<select name="taste['.$i.']">';
foreach($tastes as $t) {
echo '<option value="'.htmlentities($t).'">'.$t.'</option>';
}
echo '</select>';
echo '</p>';
}
echo '<input type="submit" name="submit" value="Submit" />';
?>
Process Code
Notable changes: I'm checking to make sure the fruit has a value, otherwise it won't insert. I'm using that arbitrary value (which is the array key $k
) to select the taste that matches the fruit.
<?php
if($_POST['submit']) {
foreach($_POST['fruit'] as $k => $v) {
if(!empty($v)) {
$query = "INSERT INTO fruits (fruit, taste) VALUE ('".mysql_real_escape_string($v)."', '".mysql_real_escape_string($_POST['taste'][$k])."')";
mysql_query($query);
}
}
}
?>