Php 复选框mult select array not在for循环内时不会推送到$\u POST

Php 复选框mult select array not在for循环内时不会推送到$\u POST,php,mysql,arrays,html,post,Php,Mysql,Arrays,Html,Post,这是我的免责声明 我正在开发一个函数,该函数从MYSQL数据库构建一个表,并将其显示在网页中(使用localhost XAMP)。那部分很简单,我发现没问题。但是,我尝试为复选框创建另一列,以允许我勾选要删除的条目,然后单击“删除”按钮删除所有选中的条目。以下是完整的功能: <?php // functions.php $conn = new mysqli($hn, $un, $pw, $db); if ($conn->connect_error) die ($conn->$c

这是我的免责声明

我正在开发一个函数,该函数从MYSQL数据库构建一个表,并将其显示在网页中(使用localhost XAMP)。那部分很简单,我发现没问题。但是,我尝试为复选框创建另一列,以允许我勾选要删除的条目,然后单击“删除”按钮删除所有选中的条目。以下是完整的功能:

<?php // functions.php
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die ($conn->$connect_error);

function buildTable($query) {
global $conn;  // need clarification why this works
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);

$rows = $result->num_rows;

echo "<table> <tr> <th>Delete</th> <th>stockID</th> <th>cardID</th> 
<th>categoryID</th> <th>condID</th> <th>Potential Value</th> </tr> ";

for ($j = 0 ; $j < $rows ; ++$j) {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);

带有删除按钮的最后一个表单没有名为“stockID[]”的字段。这些字段属于其他表单,因此不随此表单发送

您可以将带有复选框的整个表包装成一个表单

<form method='post' action='inventory.php'>
  <table> <tr> <th>Delete</th> <th>stockID</th> <th>cardID</th>
  <th>categoryID</th> <th>condID</th> <th>Potential Value</th> </tr>
  <?php
  for ($j = 0 ; $j < $rows ; ++$j) {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);
    echo "<tr> <td><input type='checkbox' name='stockID[]' value='$rows[0]'></td>";
    for ($l = 0 ; $l < 5 ; ++$l) echo "<td>$row[$l]</td>";
    echo "</tr>";
  };
  </table>
  <input type='hidden' name='delete' value='true'>
  <input type='submit' value='Delete Record(s)'>
</form>

删除stockID卡
类别条件势值

带有“删除”按钮的最后一个表单没有名为“stockID[]”的字段。这些字段属于其他表单,因此不随此表单发送

您可以将带有复选框的整个表包装成一个表单

<form method='post' action='inventory.php'>
  <table> <tr> <th>Delete</th> <th>stockID</th> <th>cardID</th>
  <th>categoryID</th> <th>condID</th> <th>Potential Value</th> </tr>
  <?php
  for ($j = 0 ; $j < $rows ; ++$j) {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);
    echo "<tr> <td><input type='checkbox' name='stockID[]' value='$rows[0]'></td>";
    for ($l = 0 ; $l < 5 ; ++$l) echo "<td>$row[$l]</td>";
    echo "</tr>";
  };
  </table>
  <input type='hidden' name='delete' value='true'>
  <input type='submit' value='Delete Record(s)'>
</form>

删除stockID卡
类别条件势值

请阅读我们中很少有人有时间或倾向于在这种漫谈中一路阅读。很可能你会根据人们在你的代码中认为错误的第一件小事得到答案,这与你在这里实际问的问题几乎没有或根本没有关系,正如我在文章末尾所说:)。仍在学习哪些信息是必要的,哪些不是。谢谢你的链接,我会读的。请阅读我们很少有人有时间或倾向于通过这种漫谈阅读。很可能你会根据人们在你的代码中认为错误的第一件小事得到答案,这与你在这里实际问的问题几乎没有或根本没有关系,正如我在文章末尾所说:)。仍在学习哪些信息是必要的,哪些不是。谢谢你的链接,我会看的。修复,谢谢:)。有两种不同的形式确实是个问题。已修复,谢谢:)。有两种不同的形式确实是个问题。
<?php //inventory.php <-- NOTICE NEW FILE
if ( isset($_POST['delete']) && isset($_POST['stockID']) ) {
  $stockID = $_POST['stockID'];
  foreach ($stockID as $value) {
    $query = 'DELETE FROM `itemized inventory` 
                    WHERE stockID="$value" ';
    queryMysql($query);
  };
}; 
<?php //function.php
function queryMysql($query) {
global $conn;
$result = $conn->query($query);
if (!$result) echo "Database action failed: $query" . "<br>" .
    $conn->error . "<br><br>"; 
};
?>
<form method='post' action='inventory.php'>
  <table> <tr> <th>Delete</th> <th>stockID</th> <th>cardID</th>
  <th>categoryID</th> <th>condID</th> <th>Potential Value</th> </tr>
  <?php
  for ($j = 0 ; $j < $rows ; ++$j) {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);
    echo "<tr> <td><input type='checkbox' name='stockID[]' value='$rows[0]'></td>";
    for ($l = 0 ; $l < 5 ; ++$l) echo "<td>$row[$l]</td>";
    echo "</tr>";
  };
  </table>
  <input type='hidden' name='delete' value='true'>
  <input type='submit' value='Delete Record(s)'>
</form>