0

I want to have my form submit several rows one for each number in the number field.

HTML Form

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

1
  • 2
    You can separate values with explode() function. is that a solution for you? Commented Dec 6, 2021 at 20:19

2 Answers 2

2

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();
?>
Sign up to request clarification or add additional context in comments.

Comments

1
$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)"
}

Comments

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.