我有一个页面,我使用PHP和HTML从Mysql渲染值表。
我想删除单击删除按钮时选中复选框的行。不幸的是,单击按钮时没有任何反应,我完全不知道在哪里看。
代码如下(数据库连接已被省略,但它有效)。
<body>
<?php
$delete = $_POST['checkbox'];
$query = "SELECT * FROM Cards";
$result = mysql_query($query);
echo "<table>";
echo "<tr><td>Name</td><td>Quantity</td><td>Color</td><td>Type</td></tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr><td><input type='checkbox' name='checkbox[]" . $row['id'] . "]'></td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Quantity'] . "</td>";
echo "<td>" . $row['Color'] . "</td>";
echo "<td>" . $row['CardType'] . "</td></tr>";
}
mysql_free_result($result);
echo "</table>";
?>
</body>
<tr>
<td colspan="5" align="center"><input name="delete" type="SUBMIT" id="delete" value="delete" action="POST"></td>
</tr>
<?php
if (isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i = 0; $i < $count; $i++) {
$id = (int) $checkbox[$i]; // Parse your value to integer
if ($id > 0) { // and check if it's bigger then 0
mysql_query("DELETE FROM Cards WHERE id = $id");
}
}
}
?>
<?php include "../footer.htm";?>
答案 0 :(得分:1)
试试这个:
<body>
<?php
$connect=mysqli_connect("Host","Username","Password","Database"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if (isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i=0;$i<$count;$i++){
if(!empty($checkbox[$i])){ /* CHECK IF CHECKBOX IS CLICKED OR NOT */
$id = mysqli_real_escape_string($connect,$checkbox[$i]); /* ESCAPE STRINGS */
mysqli_query($connect,"DELETE FROM Cards WHERE id = '$id'"); /* EXECUTE QUERY AND USE ' ' (apostrophe) IN YOUR VARIABLE */
} /* END OF IF NOT EMPTY CHECKBOX */
} /* END OF FOR LOOP */
} /* END OF ISSET DELETE */
$query = "SELECT * FROM Cards"; /* SELECT FROM Cards TABLE */
$result = mysqli_query($connect,$query); /* EXECUTE QUERY */
echo "<form action='' method='POST'>"; /* SUBMIT PAGE ON ITSELF */
echo "<table>";
echo "<tr><td>Name</td><td>Quantity</td><td>Color</td><td>Type</td></tr>";
while ($row = mysqli_fetch_array($result)){ /* FETCH ARRAY */
$id=mysqli_real_escape_string($connect,$row['id']);
echo "<tr><td><input type='checkbox' name='checkbox[]' value='$id'></td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Quantity'] . "</td>";
echo "<td>" . $row['Color'] . "</td>";
echo "<td>" . $row['CardType'] . "</td></tr>";
}
mysqli_free_result($result);
echo "</table>";
?>
<tr>
<td colspan="5" align="center"><input name="delete" type="SUBMIT" id="delete" value="delete" action="POST"></td>
</tr>
</form>
<?php include "../footer.htm";?>
</body>
<form>
功能。我在/ * / /中引用了我提供的代码中的解释。
答案 1 :(得分:1)
试试这个:
<body>
<?php
$delete = $_POST['checkbox'];
$query = "SELECT * FROM Cards";
$result = mysql_query($query);
echo "<form method=post action=''>";
echo "<table>";
echo "<tr><td>Name</td>
<td>Quantity</td>
<td>Color</td>
<td>Type</td></tr>";
while ($row = mysql_fetch_array($result)){
$content .= <<< END
<tr>
<td><input type="checkbox" name="checkbox" value="{$row['id']}"></td>
<td>{$row['Name']}</td>
<td>{$row['Quantity']}</td>
<td>{$row['Color']}</td>
<td>{$row['CardType']}</td>
</tr>
END;
}
echo $content;
mysql_free_result($result);
?>
<tr>
<td colspan="5" align="center"><input name="delete" type="SUBMIT" id="delete" value="delete"></td>
</tr>
</table></form>
</body>
<?php
if (isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
foreach($checkbox as $id) {
$id = (int) $id;
mysql_query("DELETE FROM Cards WHERE id = $id");
}
}
?>
<?php include "../footer.htm";?>