这个foreach声明有什么问题?

时间:2015-03-23 03:00:08

标签: php jquery mysql

<?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);

3 个答案:

答案 0 :(得分:1)

你在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>

答案 1 :(得分:0)

你可以做你喜欢的事情:

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>

答案 2 :(得分:0)

这将是其中的一部分。默认情况下,PHP的$ _POST数组深度为一级,所以当你保证能够迭代$ _POST时,$ _POST [&#39; checkbox&#39;]可能是一个数组(如果你输入的话)名称是复选框[0],复选框[foo]等),字符串或未设置。

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

此外,请确保您拥有&#34;值&#34;为每个复选框设置的属性。否则你将截断&#34; on&#34;而不是你期待的表格。