php - 这个 foreach 语句有什么问题?

标签 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);

最佳答案

您的 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 - 这个 foreach 语句有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29202659/

相关文章:

PHP:扩展静态成员数组

jquery - 如何将图标添加到div菜单

Javascript-根据 URL 的最后一个字符串动态更改背景图像

php - 我想选择一个表并使用我选择的表中的相同数据创建一个表

mysql - 单击按钮时从多个表单插入数据

php - MySql - 获取需要在当前分钟内运行的任务

php - 如何授予多个用户访问单个 Amazon S3 账户的权限并确定谁添加了文件?

php - XSS - 哪些 HTML 标签和属性可以触发 Javascript 事件?

javascript - 当用户单击按钮时动态加载文件

php/sql JOIN 从下拉选择中创建的 $variable 中的两个数据表