【问题标题】:Insert data in database using checkbox different row使用复选框不同的行在数据库中插入数据
【发布时间】:2021-10-05 15:53:49
【问题描述】:

场景:我正在开发一个销售和库存系统,我现在处于采购订单阶段。我要做的是将复选框数据插入到 item_po 表的数据库中。

场景:我已经生成了所有低于最低库存的商品(需要补充)。我在生成的数据左侧添加了一个复选框(参见图片以供参考)。我想将该数据插入到我的 item_po 表中,并根据为采购订单检查的项目准备生成报告。这可能吗?

这是我当前的插入代码,但它不起作用。

<?php
require_once('../../inc/config/constants.php');
require_once('../../inc/config/db.php');

$itemDetailsSearchSql = 'SELECT * FROM item WHERE stock < itemMinStock';
$itemDetailsSearchStatement = $conn->prepare($itemDetailsSearchSql);
$itemDetailsSearchStatement->execute();



$output = '<table id="itemMinStockReportsTable" class="table table-sm table-bordered table-hover" style="width:100%">
            <thead>
                <tr>
                    <th style="width: 10px !important;"><input type="checkbox" name="check" class="cbxMain" onchange="checkMain(this)"/></th>
                    <th>Lookup Code</th>
                    <th>Item Name</th>
                
                    <th>Current Stock</th>
                    <th>Unit Cost</th>
                    <th>Status</th>
                    <th>Description</th>
                    <th>Minimum Stock</th>
                    <th>Vendor</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>';

// Create table rows from the selected data
while($row = $itemDetailsSearchStatement->fetch(PDO::FETCH_ASSOC)){
$itemNumber = $row['itemNumber'];
$itemName = $row['itemName'];
$stock = $row['stock'];
$unitPrice = $row['unitPrice'];
$status = $row['status'];
$description = $row['description'];
$itemMinStock = $row['itemMinStock'];
$itemVendor = $row['itemVendor'];

    $output .= '<tr>' .
                    
                    '<td>' . ' <input type="checkbox" name="check[]" class="check"  '. '</td> '. 
                    '<td>' . $itemNumber . '</td>' .
                    //'<td>' . $row['itemName'] . '</td>' .
                    '<td><a href="#" class="itemDetailsHover" data-toggle="popover" id="' . $row['productID'] . '">' . $itemName . '</a></td>' .
    
                    '<td>' . $stock. '</td>' .
                    '<td>' . $unitPrice. '</td>' .
                    '<td>' . $status . '</td>' .
                    '<td>' . $description . '</td>' .
                    '<td>' . $itemMinStock . '</td>' .
                    '<td>' . $itemVendor . '</td>' .
                    '<td>' .'<button type="button" id="deleteItem" class="btn btn-danger">Delete</button> '. '</td>' .
                '</tr>';
}

$itemDetailsSearchStatement->closeCursor();

$output .= '</tbody>
                <tfoot>
            
                <br>
                <th> <br>   <button type="button" id="addItem" name="btn_po" class="btn btn-success">Proceed to PO</button> </th>
            
                 </tfoot>
            </table>';
echo $output;


if(isset($_POST['btn_po']))
{
   $checkbox = $_POST['check'];         
   for($i=0;$i<count($checkbox);$i++)
   {
        $check_id = $checkbox[$i];


        $stockSql = 'SELECT stock FROM item WHERE itemNumber=:itemNumber';
            $stockStatement = $conn->prepare($stockSql);
            $stockStatement->execute(['itemNumber' => $itemNumber]);
            if($stockStatement->rowCount() > 0){
                //$row = $stockStatement->fetch(PDO::FETCH_ASSOC);
                //$quantity = $quantity + $row['stock'];
                echo '<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button>Item already exists in DB. Please click the <strong>Update</strong> button to update the details. Or use a different Item Number.</div>';
                exit();
            } else {
                // Item does not exist, therefore, you can add it to DB as a new item
                // Start the insert process
                $insertItemSql = 'INSERT INTO item_po(itemNumber,itemName,stock,unitPrice,status,description,itemMinStock,itemMaxStock,itemVendor) VALUES (:itemNumber, :itemName,:stock, :unitPrice, :status, :description, :itemMinStock, :itemVendor)';

                $insertItemStatement = $conn->prepare($insertItemSql);
                $insertItemStatement->execute(['itemNumber' => $itemNumber, 'itemName' => $itemName, 'stock' => $stock, 'unitPrice' => $unitPrice, 'status' => $status, 'description' => $description, 'itemMinStock' => $itemMinStock, 'itemVendor' => $itemVendor   ]);

                echo '<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button>Item failed to insert in database.</div>';
                exit();
        }
    }
}

?>

【问题讨论】:

  • 您还需要将数据包装在 中的 &lt;input&gt; 标记中,给 字段数组名称name='data1[]' 然后使用 $_POST 或 @ 获取此值987654329@表单提交后。
  • 这能回答你的问题吗? Multiple row insert to table if check box is selected。请注意,您的代码对SQL Injections 开放。您应该改用参数化的预处理语句。

标签: php mysql


【解决方案1】:

您的checkbox 应该在&lt;form&gt; 标记内。

此外,您需要使用&lt;input&gt; 标记包装您需要的所有数据,并为每一列提供字段数组名称,如name=data1[],以便您可以取回提交表单后的数据。 (您可以使用 $_GET[ ]$_POST[ ] 方法)。

更新样本#1:已完成的表格

只是一个样本。您可以在foreach() 中添加您的MySQL INSERT,它将遍历具有复选框checked 的每一行。

<!DOCTYPE html>
<html>
<head>
    <title>Multiple Checkbox Form: Insert data only when Checkbox is checked</title>
<style type="text/css">
    table{
        width:250px;
    }
    table td {
        padding:5px;
        border: 1px solid black;
    }
</style>
</head>
<body>
<form action="" method="post">
    <table id="table">
        <tr>
            <?php $id = 1; ?>
            <td> <input type="checkbox" name="check[]" value="<?php echo $id; ?>" <?= in_array('1',$_POST['check'])? 'checked' : ''; ?> ></td>
            <td><input type="hidden" name="name[<?php echo $id; ?>]" placeholder="name" value="Name1"/>Name1</td>
            <td><input type="hidden" name="age[<?php echo $id; ?>]" placeholder="age" value="Age1">Age1</td>
            <td><button class="delBtn">Delete</button></td>
        </tr>
        <tr>
            <?php $id = 2; ?>
            <td> <input type="checkbox" name="check[]" value="<?php echo $id; ?>" <?= in_array('2',$_POST['check'])? 'checked' : ''; ?>></td>
            <td><input type="hidden" name="name[<?php echo $id; ?>]" placeholder="name" value="Name2"/>Name2</td>
            <td><input type="hidden" name="age[<?php echo $id; ?>]" placeholder="age" value="Age2">Age2</td>
            <td><button class="delBtn">Delete</button></td>
        </tr>
        <tr>
            <?php $id = 3; ?>
            <td> <input type="checkbox" name="check[]" value="<?php echo $id; ?>" <?= in_array('3',$_POST['check'])? 'checked' : ''; ?>></td>
            <td><input type="hidden" name="name[<?php echo $id; ?>]" placeholder="name" value="Name3"/>Name3</td>
            <td><input type="hidden" name="age[<?php echo $id; ?>]" placeholder="age" value="Age3">Age3</td>
            <td><button class="delBtn">Delete</button></td>
        </tr>

    </table>
  <input type="submit" name="submit" value="Proceed to PO">
</form>

<div id="result">
    <p>Data sent to PO DB:</p>
    <ol>
        <?php
            if (isset($_POST['submit'])){
                $name=$_POST['name']; //array received
                $age=$_POST['age']; //array received
                $checks=$_POST['check']; //array received (checked only)
                $i = 0;
                /* Foreach checked checkbox: Echo each data row */
                /* Add your Insert Query Here */
                foreach($checks as $key=>$check) //loop through all checks that are sent
                {
                    echo "<li>[Name ".$check."] = ".$name[$check].", [Age ".$check."] = ".$age[$check]."</li>";
                }
            }
        ?>
    </ol>
</div>
</body>
</html>

【讨论】:

  • 您好先生,感谢您回答我的问题。我只需要插入所有检查过的数据。根据您的回答,我需要用数组名称填写我的 吗?我试过了,但没有运气先生,它没有在数据库中插入数据。
  • 是的,我知道,需要插入已检查的数据。您需要使用&lt;input&gt; 标签包装您的数据。例如:应该是&lt;td&gt;&lt;input type='text' name='data1[]' value='Data'/&gt;Data&lt;/td&gt;,而不是&lt;td&gt;Data&lt;/td&gt;。你也可以使用type='hidden'
  • 我要试试这个。请稍等,先生,我真的需要这个来完成T.T
  • 好的,让我了解您的最新进展。同时,我更新了我的答案,添加了一个完整的表格,你可以尝试运行它。基本上为每一行设置一个特定/唯一的键来命名属性。您可以使用$id 或任何键来唯一标识每一行。然后使用key这个checked复选框获取每一行的数据。
  • 您好,先生,抱歉回复晚了。我无法应用代码,因为我使用 ajax 我如何将它应用到 ajax。这就是为什么我没有表格的原因,因为我使用的是 ajax。
猜你喜欢
相关资源
最近更新 更多
热门标签