Nabil Ghulam
Nabil Ghulam

Reputation: 211

Insert multiple rows SQL from a form with same titles

I have this form, which when you click on "New row" there are popping up another field with new inputs. What I want to achieve with this is an user-friendly form which FOREACH "New item" ->

("INSERT 
INTO menues(
    restaurant_id,
    title, 
    subtitle, 
    name, 
    ingredients, 
    price, 
    category, 
    upload_date
    )
VALUES(
    :restaurant_id,
    :title, 
    :subtitle, 
    :name, 
    :ingredients, 
    :price, 
    :category,
    NOW()
    )
");

When adding multiply rows, every row shall have the same values from the upper field of the form(title, subtitle) + restaurant_id and category BUT different values for name, ingredients and price.

In what way can I achieve this?

The form:

Before clicking When clicked "New row" >> After Clicking

Upvotes: 2

Views: 1719

Answers (2)

charmixer
charmixer

Reputation: 651

You could make your input fields return an array:

<input type="text" name="food_name[]" />
<input type="text" name="food_ingredient[]" />
<input type="text" name="food_price[]" />

and then loop through the results:

$name = $_POST["food_name"];
$ingredient = $_POST["food_ingredient"];
$price = $_POST["food_price"];
$length = count($name);
for($key=0;$key<$length;$key++){
 echo $name[$key] . "<br/>"; 
 echo $ingredient[$key] . "<br/>"; 
 echo $price[$key] . "<br/>";  
 echo "-----<br/>";
} 

That's how I did it, when I had your problem.

Upvotes: 3

jdccons
jdccons

Reputation: 1

This is a php question I would think. Read values from your form into an array. Then run another code block to insert the array into your table using the for each construct and insert the elements of the array using your sql statement into your table iteratively until the array is exhausted.

Upvotes: 0

Related Questions