php - MySQL 使用 while 循环仅插入最后一行

标签 php mysql foreach while-loop

我有一个 while 循环。它位于表单内。我想在提交提交按钮时插入 while 循环中的所有行。下面是while循环的代码。

<form method="POST" action="insert.php" n>
  <table>
                    <?php                           
                     $i =0;
                     $link = mysqli_connect('localhost', 'root', '', 'shibu');
                     $sql = "select `cus_id`, `mobile`, `date`, `time`,
                            sum(`d1` + `d2` + `d3`) as `credit`,
                            from `finale`;
                        $result=mysqli_query($link, $sql);
                        $count = mysqli_num_rows($result);
                        if($count == 0){
                        ?>
                        <tr> 
                        <th><?php echo 'No Records Founds!' .mysqli_error($link); ?></th> 
                        </tr>
                        <?php 
                        }else{
                            while($sql_result = mysqli_fetch_assoc($result)){
                        ?>
<tr>
<td><?php echo $i += 1; ?></td>
<td><input type="text" name="cus_id" value="<?php echo $sql_result['cus_id']; ?>" ></td>
<td><input type="text" name="mobile" value="<?php echo $sql_result['mobile']; ?>" ></td>
<td><input type="text" name="date" value="<?php echo $sql_result['date']; ?>" ></td>
<td><input type="text" name="time" value="<?php echo $sql_result['time']; ?>"</td>
<td><input type="text" name="credit" value="<?php echo $sql_result['credit']; ?>"></td>
</tr>
    <?php
    }
    }
    ?>
 </table>
<input name="submit" type="submit"  />
</form>

这是我的插入查询。

<?php

$link = mysqli_connect('localhost', 'root', '', 'shibu');

$stmt = mysqli_prepare($link, "INSERT INTO single 
                 (`cus_id`, `mobile`, `date`, `time`, `credit`) VALUES (?,?,?,?,?)");

mysqli_stmt_bind_param($stmt, 'isssi', $cus_id, $mobile, $date, $time, $credit);

foreach ((array)$_POST['cus_id'] as $i => $cus_id) {
                $mobile = $_POST['mobile'];
                $date = $_POST['date'];
                $time = $_POST['time'];
                $credit  = $_POST['credit'];

mysqli_stmt_execute($stmt);
                }

if(!$stmt){ 
echo "error". mysqli_error($link);
}

我的问题是当我输入提交按钮时,只有最后一行输入而不是整行。

我衷心需要你的帮助。

最佳答案

在您的 html 代码中,您需要将输入名称定义为数组。

喜欢

<td><input type="text" name="cus_id[]" value="<?php echo $sql_result['cus_id']; ?>" ></td>
<td><input type="text" name="mobile[]" value="<?php echo $sql_result['mobile']; ?>" ></td>
<td><input type="text" name="date[]" value="<?php echo $sql_result['date']; ?>" ></td>
<td><input type="text" name="time[]" value="<?php echo $sql_result['time']; ?>"</td>
<td><input type="text" name="credit[]" value="<?php echo $sql_result['credit']; ?>"></td>

在 php 中使用它们时,您需要循环它们并运行插入查询

foreach ($_POST['cus_id'] as $i => $value) {
    $cus_id = $_POST['cus_id'][$i];
    $mobile = $_POST['mobile'][$i];
    $date = $_POST['date'][$i];
    $time = $_POST['time'][$i];
    $credit  = $_POST['credit'][$i];

    // Yourinsert query
}

关于php - MySQL 使用 while 循环仅插入最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46853814/

相关文章:

mysql - mysql中::numeric和::text是什么意思

mysql - 终端中的 MAMP Mysql

php - 只为 foreach 循环显示一条消息

python - 在 Python 3 中不使用 `break` 停止迭代

php - 列出数组 php 中的数字

php - IDE 代码模板可以提高我的工作效率吗?

javascript - 如何在 JavaScript ajax 调用中从 PHP passthru 获取二进制数据?

php - 将我的 RESTful API 仅限于我的应用程序

mysql - 获取三个不同表中包含相同 id 的所有行

php - 如何让apache2用户访问/root目录下的文件?