I want to have my form submit several rows one for each number in the number field.
In the number field, I want to enter several number separated by commas (e.g. 10,8,95,109), then a single comment (e.g. Hello). When submitted, that should insert one row per number into the db.
So in the database, this would be what is submitted.
Number | Comment |
---|---|
10 | Hello |
8 | Hello |
95 | Hello |
109 | Hello |
I would also like this to only accept number (and commas etc).
Thanks
CodePudding user response:
$inputNumbers = "10,8,95,109";
$comment = "Hello";
$inputNumbers = explode(",",$inputNumbers);
foreach($inputNumbers as $inputNumber){
//use db query to insert for each inputNumber
//"INSERT INTO <tableName> (Number, Comment)
//VALUES ( $inputNumber, $comment)"
}
CodePudding user response:
This should work. The actual implementation depends on your actual setup and your DB implementation.
<?php
$numb_str = "10,8,95,109";
$comment = "Hello";
$numb_arr = explode(',', $numb_str);
$stmt = $connect->prepare("INSERT INTO your_table (numb, comment) VALUES (?, ?)");
$stmt->bind_param("ss", $numb, $comment);
for ($i = 0; $i < count($numb_arr); $i ) {
$numb = $numb_arr[$i];
$stmt->execute();
}
$stmt->close();
?>