1

Hi I want to add a lot of inputs with same name to my database.

I am working on a recipe submit form, that adds in ingredients with form input text. I have multiple inputs with same name, and want them all to be added to the database. By array of some sort.

I have a jquery that makes it possible to add in more ingredients and amount, don't think it is important for this question. So won't add.

Till now I have this html/php:

<form id="opskriftReg" name="opskriftReg" action="opskriftRegSave.php" method="post">

*Ingredienser:<br>

Ingrediens: <input type="text" name="ingredients[]"> 
Mængde: <input type="text" name="amount[]"><br>

<div id="InputsWrapper"></div>

<input type="button" id="AddMoreFileBox" value="Tilføj ingrediens"><br>

<input type="submit" value="Submit">

</form>

And this for php/database input:

$mysqli = new mysqli ("localhost","","","brugerreg");

//Add this php add to database: 
$ingredients = $_POST['ingredients'];
$amount = $_POST['amount'];

echo $ingredients." ".$amount;

$sql = "INSERT INTO `brugerreg`.`opskriftreg` (`ingredients`,
`amount`) VALUES ('".$ingredients."', '".$amount."')"; 

$stmt = $mysqli->prepare($sql); $stmt->execute();
3
  • Possible duplicate. Moroever if you should add an array, maybe you have a database conception issue. Commented Apr 1, 2014 at 11:50
  • The database connection work, and how do I add and array to database? At the moment when I submit, it just writes "array" in both collums Commented Apr 1, 2014 at 11:52
  • If you read the link I've give : You can not insert a array directly to mysql as mysql doesn't understand php data types. Commented Apr 1, 2014 at 11:53

2 Answers 2

2

Make your jQuery print your inputs such as:

<input type="text" name="ingredients[]"> 
<input type="text" name="amount[]">

Note the [] in the name, these are called HTML input arrays.

Now you can access these inputs in your PHP as:

$ingredients = implode(',',$_POST['ingredients']);
$amount = implode(',',$_POST['amount']);
echo $ingredients."<br>".$amount; //you could comment this
$sql = "INSERT INTO `brugerreg`.`opskriftreg` (`ingredients`,
`amount`) VALUES ('".$ingredients."', '".$amount."')"; 
$stmt = $mysqli->prepare($sql); $stmt->execute();

You could use the implode() function to convert an array into a single string with a delimiter

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

1 Comment

It worked, more or less :-) Is there a possible way to add all ingredients to one row? At the moment it creates multiple rows each ingredient. Like a string seperated by commas?
0

Found here.

Every time you add new input with same name, append it with "[]", so in the end you get:

Ingrediens: <input type="text" name="ingredients[]"> 
Mængde: <input type="text" name="amount[]"><br>
Ingrediens: <input type="text" name="ingredients[]"> 
Mængde: <input type="text" name="amount[]"><br>
Ingrediens: <input type="text" name="ingredients[]"> 
Mængde: <input type="text" name="amount[]"><br>

And in php:

$ingredients = $_POST['ingredients']; // $ingredients is now an array
$amount = $_POST['amount']; // $amount is now an array

echo $amount[0];
echo $amount[1];

To insert it into database just prepare the query accordingly, for example iterate over the array and concatenate the "('".$ingredients."', '".$amount."')" for every pair.

$values = "". 
for ($i = 0; $i < sizeof($amount); $i++) {
    $values .= "('".$ingredients[$i]."', '".$amount[$i]."')";
    if ($i != sizeof($amount) - 1) {
        $values .= ", ";
    }
}


$sql = "INSERT INTO `brugerreg`.`opskriftreg` (`ingredients`,`amount`) VALUES " . $values; 

2 Comments

I think this is what I want. Is it possible you can show and example of your concatenate version. If you understand :-) ?
It's in the edited post, I concatenate the content of values in parenthesis in for loop, separate them with coma if needed and put into your insert. I doubt it's the best way but it should be good enough for you needs.

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.