简体   繁体   中英

insert multiple rows to a table using foreach php

i need to insert multiple rows data into table Date and inspection_id will remain same but the input values are repeated. Here is the script i have

          <?php
if (isset($_POST["submit"])) {
$taskdate = date('Y:m:d');  
$inspection_id = '1';
$post = $_POST['nfo'];
foreach ($post['point_id'] as $key => $value) {
$point_id.= $value.", ";
}
foreach ($post['point_comment'] as $key => $value) {
$point_comment.= $value.", ";
}

foreach ($post['point_value'] as $key => $value) {
$point_value.= $value.", ";
}
$query = "INSERT INTO `inspections` (`inspection_id`, `point_id`, `value`, `comment`)VALUES('$inspection_id', '$point_id', '$point_value', '$point_comment')";
$result = mysql_query($query);

HTML FORM i am USING

                                            <form action="pentasks.php" method="post">
                                                    <select name="nfo[point_value][]">
                                                        <option selected>Chose</option>
                                                            <option value="1">Qualify</option>
                                                            <option value="2">Disqualify</option>
                                                    </select>
                                                        <input name = "nfo[point_comment][]" value = "" type="text">
                                                        <input type="hidden" name="inspection_id" value="<?= $task_id; ?>"> <!-- value From another query -->
                                                        <input type="hidden" name="nfo[point_id][]" value="<?= $spot_id3; ?>">

                    <input type="submit" name="submit" value="Submit">
                </form>

Help please how i insert data this is something in a table questions are defined retrieved on form with input to answer

Try this

foreach ($post['point_comment'] as $key => $value) {
$point_comment=$post['point_comment'][$value];
$point_value= $post['point_value'][$value];
//run your query here
}

and mysql is depricated Learn mysqli_ function or PDO.

and change your select tag like this

<select name="point_value[]">
<select name="point_comment[]">

Arif thanks for your effort but your answer did not helped spend few hours by ownself i got it working and here is the working solution for my question

HTML FORM

<input type = "text" name = "point_comment[]" />
<input type = "text" name = "point_value[]" />

Here is Foreach loop PHP

$my_comment = $_POST['point_comment'];
$point_comment = "";
foreach ($my_comment as $key => $value) {
$point_comment = $value;

$my_value = $_POST['point_value'];
$point_value = "";
foreach ($my_value as $key => $value) {
$point_value = $value;
// SQL INSERT or anything query here
} }

Hope this will help for those looking for similar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM