简体   繁体   English

使用PHP将while循环中的数据插入表中

[英]Insert data from while loop into a table with php

I'm creating a form using HTML and PHP. 我正在使用HTML和PHP创建表单。 I have created a form which I want to submit and save that data in database. 我已经创建了一个表单,我想提交该数据并将其保存在数据库中。

I'm trying to submit a form with data that comes from a while loop. 我正在尝试使用来自while循环的数据提交表单。 All input values are getting generated by while loop. 所有输入值都由while循环生成。

The code looks like this. 代码看起来像这样。

<table width="1348" border="0" class="table table-striped" >
        <tr>
          <td width="106">&nbsp;</td>

          <td width="332"><strong>Product Code</strong></td>
          <td width="375"><strong>Product Name</strong></td>
          <td width="211"><strong>QTY</strong></td>


      </tr>
                <?php
    $i = 0;
    $rowset = mysql_query("select * from product_detail where productID='".$data['productCode']."'");
    while($stuff = mysql_fetch_array($rowset)){
    ?>
    <tr>

        <td><input type="text" name="code[<?php echo $i?>]" value="<?php enter code hereecho $stuff['code'];?>"/></td>
        <td><input type="text" name="name[<?php echo $i?>]" value="<?php echo $stuff['name'];?>" size="50"/></td>
        <td><input type="text" name="qty[<?php echo $i?>]"  value="<?php echo $stuff['qty'];?>" size="10"/></td>

    </tr>
    <?php $i++; }?>
    <tr id="last">
</table>
<input type="submit" name="save id="save" class="btn btn-primary btn-lg"/>

This is the code to add the data to database. 这是将数据添加到数据库的代码。

$code=$_POST['code'.$i];
$name=$_POST['name'.$i];
$qty=$_POST['qty'.$i];
$query = mysqli_query($con,"insert into stock(productCode, productName, qty) values ('".$code."', '".$name."','".$qty."')") or die(mysqli_error($con));

First, use prepared statement with bind_param as your script is totally exposed to sql injection. 首先,将prepared语句与bind_param一起使用,因为您的脚本完全暴露于sql注入。

Second, you can add input type hidden for the number of rows 其次,您可以为行数添加隐藏的输入类型

<form action="" method="POST">
    <table width="1348" border="0" class="table table-striped" >
                <tr>
                        <td width="106">&nbsp;</td>
                        <td width="332"><strong>Product Code</strong></td>
                        <td width="375"><strong>Product Name</strong></td>
                        <td width="211"><strong>QTY</strong></td>
                </tr>
<?php
    $data['productCode'] = "1"; // sample data
    $stmt = $con->prepare("SELECT * FROM product_detail WHERE productID = ?");
    $stmt->bind_param("i", $data['productCode']);
    $stmt->execute();
    $result = $stmt->get_result();
    $i = 0;
    while($stuff = $result->fetch_assoc()) {
?>
            <tr>
                    <td></td>
                    <td><input type="text" name="code[<?php echo $i; ?>]" value="<?php echo $stuff['code'];?>"/></td>
                    <td><input type="text" name="name[<?php echo $i; ?>]" value="<?php echo $stuff['name']; ?>" size="50" /></td>
                    <td><input type="text" name="qty[<?php echo $i; ?>]"  value="<?php echo $stuff['qty']; ?>" size="10" /></td>

            </tr>
<?php
        $i++;
    }
?>
                    <input type="hidden" name="count" value="<?php echo $i; ?>" />
            <tr id="last">
    </table>
<input type="submit" name="save" id="save" class="btn btn-primary btn-lg"/>
</form>

post count with the form 带有表格的计数

<?php
if (isset($_POST['save'])) {
    $count = $_POST['count'];
    for ($i = 0; $i < $count; $i++) {
        $code = $_POST['code'][$i]; // check empty and check if interger
        $name = $_POST['name'][$i]; // check empty and strip tags
        $qty = $_POST['qty'][$i]; // check empty and check if interger

        $stmt = $con->prepare("INSERT INTO stock (productCode, productName, qty) VALUES (?, ?, ?)");
        $stmt->bind_param("iss",$code,$name,$qty);
        $stmt->execute();
    }
}
?>

You may also want to check if post values are empty with other necessary validation before insert 您可能还需要在插入之前通过其他必要的验证来检查发布值是否为空

Since the table is dynamically filled, you need to use an array as the name attribute 由于表是动态填充的,因此您需要使用数组作为name属性

<table>
        <tr>
            <th>Name</th>
            <th>Present</th>
            <th>Excused</th>
            <th>Unexcused</th>
            <th>Ext</th>
        </tr>
        <?php         
        $query = "select * from TbCard";
        $sql = mysqli_query($connect, $query);
        $count = 0;
            while ($data = mysqli_fetch_array($sql)) {
        ?>
                <tr>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['dataName']" id='name' type='text' value="<?php echo $data['Name'];?>" readonly style='border:none;width:350px'></input>
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Present"> Present
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Excused"> Excused
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Unexcused"> Unexcused
                </td>
                </tr>;
        <?php
             $count++;
            }
        ?>
    </table>

The php would be something like this, assuming that the data has values in it: 假设数据中包含值,则php将是这样的:

$tableRow = $_POST['tableRow'];
foreach($tableRow as $row){
   /* here insert data from post */
    echo $row['dataName'].' '.$row['status'].'<br/>';
}

To see the content of the array, use print_r($tableRow) in this case i use a name tableRow 要查看数组的内容,请使用print_r($ tableRow)在这种情况下,我使用名称tableRow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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