[英]Checkbox mult-select array not will not push to $_POST while inside a for loop
长篇文章,所以这是我的免责声明。
我正在使用从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);
这部分代码是我的问题; 当我在操作页面'inventory.php'上使用print_r($ _ POST)时,$ _ POST数组不显示stockID存在。
echo "<tr> <td> <form method='post' action='inventory.php'>
<input type='checkbox' name='stockID[]' value='$rows[0]'>
</form></td>";
for ($l = 0 ; $l < 5 ; ++$l) echo "<td>$row[$l]</td>";
echo "</tr>";
};
结束可能的问题位,继续其余代码:
echo <<<_END
?>
</table> <a href='card input.php'> Record inventory </a>
<form method='post' action='inventory.php'>
<input type='hidden' name='delete' value='true'>
<input type='submit' value='Delete Record(s)'>
</form>
_END;
};
然后,我在操作页面上使用此IF语句来确定要删除的内容:
<?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);
};
};
我访问过的可能问题:
1.) stockID用于同一目录中的另一个文件,并且有冲突。 我不认为如果两个相同的关联数组键位于未通过“ require”或“ include”调用连接的两个不同文件上,则不会发生冲突。 我是新手,所以我可能是错的。 Ive还尝试了其他ID,但没有解决该问题。
2.) 我的将DELETE命令调用到SQL中的IF语句不正确。 这可能仍然是正确的,但是我认为我什至不能肯定地说出来,因为由于我的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>";
};
?>
因为我不是a。)收到错误,b。)没有从数据库中删除任何内容,所以我得出的结论是<input type='checkbox' name='stockID[]' value='$row[0]'>
从'functions.php页面'上没有被推到操作页面'inventory.php'上的$ _POST数组中(通过在'inventory.php'页面上使用print_r($ _ POST)进一步确认)。 因为isset($ _ POST ['stockID'])然后为false,所以用于确定要删除的内容的IF语句甚至不被调用,因为它的条件为false。 因此,查询甚至没有被提交,因此我无法证明这是问题所在。
3) 对于循环和heredocs不是朋友。 也许? 在这一点上,我只是在猜测...。也许不是朋友的for循环和<input>
语句?
那就是我能想到的。 任何帮助将不胜感激。 Newb程序员和发布者放在此处,因此请轻松进行。 粗糙度也可以,之后我们依sn在一起。
谢谢!
最后一个带有“删除”按钮的表单没有名为“ 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>
或在发送最后一个表单之前调用javascript函数,收集所有选中的复选框的值,然后将其传递到发送的数据中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.