在 while 循环 php 中插入表单中的数据

再会。我只是想问如何插入 while 循环生成的表单中的数据。这是我到目前为止所尝试的。我添加了一个循环,其中它们将具有不同的 ID 或名称,但是当我尝试单击按钮时,第一个表单是唯一有效的。预先非常感谢您。


<?php

include "../config/dbconfig.php";


$data['productCode'] = "1"; // sample data

$stmt = $conn->prepare("SELECT * FROM tbl_category");

//$stmt->bind_param("i", $data['productCode']);

$stmt->execute();

$result = $stmt->get_result();

$i = 1;

while ($stuff = $result->fetch_assoc()) {

?>

    <div class="col-sm-6" style="margin-top:20px;">

        <div class="card">

            <div class="card-header"><?php echo $stuff['categoryname']; ?>

            </div>

            <div class="card-body outermydiv">

                <div class="myDIV">

                    <form method="POST" name="itemform" action="">

                        <div class="form-row">

                            <div class="col-5">

                                <input type="text" class="form-control" name="name[<?php echo $i; ?>]" id="itemname[<?php echo $i; ?>]" placeholder="Item name" required autocomplete="off">

                            </div>

                            <div class="col">

                                <input type="number" class="form-control" name="cost[<?php echo $i; ?>]" id="itemcost[<?php echo $i; ?>]" placeholder="Cost" required>

                            </div>


天涯尽头无女友
浏览 97回答 1
1回答

杨__羊羊

从重写你的表格开始......你不需要$i任何东西,但我会留下声明,以防你需要它做其他事情。不要提交数组类型数据,每个表单都会提交自己的一组字段。$stuff['categorycode']添加为value每个提交的 以避免需要隐藏字段可能更有意义。我现在就让你自己做吧。形式:foreach ($stmt->get_result() as $i => $stuff) { ?>&nbsp; &nbsp; <div class="col-sm-6" style="margin-top:20px;">&nbsp; &nbsp; &nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-header"><?php echo $stuff['categoryname']; ?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body outermydiv">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="myDIV">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" name="name" placeholder="Item name" required autocomplete="off">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" class="form-control" name="cost" placeholder="Cost" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" class="form-control" name="price" placeholder="Price" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-success" name="btnsaveitem">Save</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" class="form-control" name="code" value="<?php echo $stuff['categorycode']; ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <?php}&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;接收脚本:(根据需要扩展其他字段)if (isset($_POST['btnsaveitem'])) {&nbsp; &nbsp; $stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES (?)");&nbsp; &nbsp; $stmt->bind_param("s",$_POST['name']);&nbsp; &nbsp; $stmt->execute();}这都是未经测试的代码。
打开App,查看更多内容
随时随地看视频慕课网APP