Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
share|improve this question
    
Possible duplicate. Moroever if you should add an array, maybe you have a database conception issue. –  Debflav Apr 1 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 –  mus Apr 1 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. –  Debflav Apr 1 at 11:53

2 Answers 2

up vote 0 down vote accepted

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

share|improve this answer
    
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? –  mus Apr 1 at 12:12
    
@mus Sure you can, have a look at the edited code. –  I Can Has Kittenz Apr 1 at 12:15
    
THANK YOU! it works! THANK YOU THANK YOU! –  mus Apr 1 at 12:28
    
@mus YOU'RE WELCOME! –  I Can Has Kittenz Apr 1 at 12:28

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; 
share|improve this answer
    
I think this is what I want. Is it possible you can show and example of your concatenate version. If you understand :-) ? –  mus Apr 1 at 12:01
    
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. –  mareckmareck Apr 1 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.