这句话有什么问题


What is wrong with this foreach statement?

<?php
include ('config.php');
$connection = mysql_connect($mysql_host, $mysql_user, $mysql_password);
$db = mysql_select_db($mysql_database, $connection);
$machinename = $_POST['machinename'];
$safemachinename = mysql_real_escape_string($machinename);
$query = mysql_query("delete from Notification where machinename=$safemachinename", $connection);
$query = mysql_query("delete from Keystrokes where machinename=$safemachinename", $connection);
$query = mysql_query("delete from Clipboard where machinename=$safemachinename", $connection);
$query = mysql_query("delete from Passwords where machinename=$safemachinename", $connection);
foreach($_POST['checkbox'] as $checkbox){
    $query = mysql_query("TRUNCATE TABLE $checkbox", $connection);
}
?>

我收到错误"为 foreach(( 提供的参数无效。是因为我在测试时没有选中任何复选框吗?

这看起来正确吗?

if(isset($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $checkbox){
    $query = mysql_query("TRUNCATE TABLE $checkbox", $connection);
}
}

这是复选框

<form method="post" name="checkDelete" id="checkDelete" action="deletelogs.php">
<label class="checkbox-inline">
<input type="checkbox" name="Notifications"/> Notifications</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="Keystrokes"/> Keystrokes</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="Passwords"/> Passwords</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="Clipboard"/> Clipboard Records</label><br><br>
<button type="submit" class="btn btn-primary">Delete Logs</button>
</form>

基本上,您正在通过复选框选择要删除的表。这适用于客户端管理程序。

好吧,到目前为止一切顺利。这似乎并没有删除任何内容:

<form method="post" name="formdelete" id="formdelete" action="deletelogs.php">
<div class="form-group">
                                            <label>Delete all logs where computer name is</label><input class="form-control" width = "200px" placeholder="Please input machine name here..." /><br>
<br>
<center><input type="submit" name="btnDelete" class="btn btn-primary" value="Delete now"></center>
</form>
$machinename = $_POST['machinename'];
$safemachinename = mysql_real_escape_string($machinename);
$query = mysql_query("delete from Notification where machinename=$safemachinename", $connection);
$query = mysql_query("delete from Keystrokes where machinename=$safemachinename", $connection);
$query = mysql_query("delete from Clipboard where machinename=$safemachinename", $connection);
$query = mysql_query("delete from Passwords where machinename=$safemachinename", $connection);

你在 html 部分有错误。 像这样使用

<form method="post" name="checkDelete" id="checkDelete" action="">
<label class="checkbox-inline">
<input type="checkbox" name="checkbox[]" value="Notifications"/> Notifications</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="checkbox[]" value="Keystrokes"/> Keystrokes</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="checkbox[]" value="Passwords"/> Passwords</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="checkbox[]" value="Clipboard"/> Clipboard Records</label><br><br>
<button type="submit" class="btn btn-primary">Delete Logs</button>
</form>

这将是其中的一部分。默认情况下,PHP 的 $_POST 数组深度为一级,因此虽然您保证能够迭代超过 $_POST,但 $_POST['checkbox'] 可能是数组(如果您的输入名称是 checkbox[0]、checkbox[foo] 等(、字符串或未设置。

所以你需要在foreach之前做一个isset((&&is_array((,以确保你的foreach不会失败。

此外,请确保为每个复选框设置了"value"属性。否则,您将截断"on"而不是您期望的表。

你可以

做你喜欢的事情,做:

if(isset($_POST['info'])){
      foreach($_POST['info'] as $checkbox){
      if(isset(checkbox)){
          $query = mysql_query("TRUNCATE TABLE $checkbox", $connection);
     }
   }
}
<form method="post" name="checkDelete" id="checkDelete" action="deletelogs.php">
<label class="checkbox-inline">
<input type="checkbox" name="info[]" value="Notifications"/> Notifications</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="info[]" value="Keystrokes"/> Keystrokes</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="info[]" value="Passwords"/> Passwords</label><br>
<label class="checkbox-inline">
<input type="checkbox" name="info[]" value="Clipboard"/> Clipboard Records</label><br><br>
<button type="submit" class="btn btn-primary">Delete Logs</button>
</form>