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();
Madvillain
  • 86
  • 1
  • 9
  • [Possible duplicate](http://stackoverflow.com/questions/10054633/insert-array-into-mysql-database-with-php). Moroever if you should add an array, maybe you have a database conception issue. – Debflav Apr 01 '14 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 – Madvillain Apr 01 '14 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 01 '14 at 11:53

2 Answers2

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

AyB
  • 11,609
  • 4
  • 32
  • 47
  • 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? – Madvillain Apr 01 '14 at 12:12
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; 
Community
  • 1
  • 1
mareckmareck
  • 1,560
  • 13
  • 18
  • I think this is what I want. Is it possible you can show and example of your concatenate version. If you understand :-) ? – Madvillain Apr 01 '14 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 01 '14 at 12:03