如何使用 PHP 使用复选框从 mysql 数据库中删除多行?
Posted
技术标签:
【中文标题】如何使用 PHP 使用复选框从 mysql 数据库中删除多行?【英文标题】:How to delete multiple rows from mysql database with checkbox using PHP? 【发布时间】:2014-07-18 01:22:55 【问题描述】:我尝试在“admin”数据库中delete
我的数据,但删除按钮不起作用。
这是我的顶部
<?php
$host="localhost"; // Host name
$username="root"; // mysql username
$password=""; // Mysql password
$db_name="admin"; // Database name
$tbl_name="admin"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
这是我的复选框代码
<tbody>
<?php
while($rows=mysql_fetch_array($result))
?>
<tr>
<td><?php echo $rows['course_code']; ?></td>
<td><?php echo $rows['course_name']; ?></td>
<td><?php echo $rows['lecture_id']; ?></td>
<td><input name="checkbox[]" type="checkbox"
id="checkbox[]" value="<?php echo $rows['course_code'];?>"></td>
<td><form>
</form>
</td>
</tr>
<?php
?>
</tbody>
还有,这是我的按钮代码
<input type='button' id="delete" value='Delete' name='delete'>
这是我的php函数代码
<?php
if(isset($_POST['delete']))
for($i=0;$i<$count;$i++)
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";
$result = mysql_query($sql);
if($result)
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
mysql_close();
?>
【问题讨论】:
您的输入超出了您的<form>
标签;第一个重大问题。另外,没有action
或方法设置为应该完成工作的文件。即:<form action="delete.php"... method = "post"
更正这将您的输入文件放入表单<td><form> </form>
请参阅与您的***.com/q/14475096 类似的关于 SO 的问答,您会在那里找到答案。这是我开始使用的,过去对我有很大帮助。
另外,如果未设置,表单方法默认为 GET。做<form action="delete.php" method="post">
- 你现在有足够的信息来自己解决这个问题。
【参考方案1】:
<?php
$args1 = array(
'role' => 'Vendor',
'orderby' => 'user_nicename',
'exclude' => $user_id.',1',
'order' => 'ASC'
);
$subscribers = get_users($args1); foreach ($subscribers as $user)
$fvendorck = $wpdb->get_row("select * from wp_vandor where parent_id = '".$user_id."' and child_id = '".$user->id."'");
$isfavvendor = $fvendorck->child_id;
if(!empty($isfavvendor))
?>
<li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" checked=""/><?php echo $user->headline; ?></li>
<?php else ?>
<li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" /><?php echo $user->headline; ?></li>
<?php ?>
</ul>
【讨论】:
【参考方案2】:$sql = "SELECT * FROM blacklist";
$result = $link->query($sql);
$count=mysqli_num_rows($result);
if ($result->num_rows > 0)
while($row = $result->fetch_assoc())
echo "<table>";
echo "<th>";
echo "<td>" . "ID: " . $row["id"]."</td>";
echo "<td>" . " Dial Target: " . $row["dial_target"]."</td>";
echo "<td>" . " Destination: " . $row["pozn"]."</td>";
echo "<td>" . " Date: " . $row["block_date"] . "</td>";
echo "<td>" . "<div class='background' style='position: relative; top:8px;'>" . "<form>" . "<input action='index.php' method='post' type='checkbox' name='chechbox[]' value='".$row["id"]."'/>" ."</form>" . "</div>" . "</td>";
echo "</th>";
echo "</table>";
echo "</br>";
else
echo "0 results";
if(isset($_POST['Delete']))
for($i=0;$i<$count;$i++)
$del_id = $checkbox[$i];
$del = "DELETE FROM blacklist WHERE Delete='$del_id'";
$result = $link->query($del);
if($result)
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
<!-- DELETE BUTTON -->
<form>
<input type='Submit' id="Delete" value='Delete' name='Delete'/>
</form>
【讨论】:
但我的按钮仍然不起作用【参考方案3】:试试这个代码。它运行良好。 connection.php
<?php $hostname_conection = "localhost"; /* this is the server name(assigned to variable) which is localhost since it runs on local machine */
$database_conection = "company"; /* this is the database name( assigned to variable)*/
$username_conection = "root"; /* user name (assigned to variable)*/
$password_conection = ""; /*password (assigned to variable) */
$conection = mysql_connect($hostname_conection, $username_conection, $password_conection) or trigger_error(mysql_error(),E_USER_ERROR); /* Mysql_connect function is used to conncet with database it takes three parameters server/hostname, username,and password*/
mysql_select_db($database_conection,$conection) or die(mysql_error("could not connect to database!")); /* Mysql_select is used to select the database it takes two parameters databasename and connection variable in this case $conection */
?>
multiple_delete.php
<?php require_once('conection.php'); ?>
<?php
in
/* now to display the data from the database which we inserted in above form we */ /* we make the query to select data from the table EMP */
$display = "select * from test_mysql";
$result = mysql_query($display, $conection) or die(mysql_error()); /* the query is executed and result of the query is stored in variable $result */
if ($result == FALSE)
die(mysql_error()); /* displays error */
?> <h1 align="center"> Displaying Recods in Table </h1>
<form method="get" action="" id="deleteform" >
<table border="1" align="center">
<tr>
<td >
<input type="submit" name="delete" id="button" value="delete" onclick="document.getElementById('deleteform').action = 'delete.php';document.getElementById('deleteform').submit();"/> <!--- here on clicking the button the form is submitted and action is set to delete.php Here we have used javascript document refers to this whole page and now we can access any tag that has its id with help of getElementById() method and after the we specify the operation we want to perform in this case action and submit. --->
</td>
<td >id</td>
<td >name</td>
<td >lastname</td>
</tr>
<?php
while ($rows = mysql_fetch_array($result))
/* here we make use of the while loop which fetch the data from the $result int array form and stores in $row now we can display each field from the table with $row[‘field_name’] as below */
?>
<tr>
<td>
<input type="checkbox" name="empids[]" value="<?php echo $rows['id']; ?>" /> <!--here with each checkbox we send the id of the record in the empids[] array --->
</td>
<td>
<?php echo $rows['id'] ?>
</td>
<td>
<?php echo $rows['lastname'] ?>
</td>
<td><?php echo $rows['name'] ?></td>
<?php ?>
</tr>
</table>
</form> ?>
</body>
</html>
删除.php
<?php
require_once('conection.php');
?>
<?php
if (isset($_GET['delete'])) /* checks weather $_GET['delete'] is set*/
if (isset($_GET['empids'])) /* checks weather $_GET['empids'] is set */
$checkbox = $_GET['empids']; /* value is stored in $checbox variable */
if (is_array($checkbox))
foreach ($checkbox as $key => $your_slected_id) /* for each loop is used to get id and that id is used to delete the record below */
$q="DELETE FROM test_mysql WHERE id=$your_slected_id "; /* Sql query to delete the records whose id is equal to $your_slected_id */
mysql_query($q,$conection) ; /* runs the query */
header("location:multiple_delete.php"); /* Goes back to index.php */
else
echo" you have not selected reords .. to delete";
?>
【讨论】:
【参考方案4】:在<form>
标签中包含所有输入元素:<form> all inputs are here </form>
更新:
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['course_code'];?>">
to(id 在这里无关紧要):
<input name="checkbox[]" type="checkbox" value="<?php echo $rows['course_code'];?>"/>
和你的按钮代码:
<input type='button' id="delete" value='Delete' name='delete'>
到
<input type="submit" value="Delete"/>
设置打开<form>
标签为<form action="delete.php" method="post">
注意: 我假设下面的代码在 delete.php 文件中。如果没有在上面的打开表单标记中用该名称替换“delete.php”。
你的 delete.php 文件:
<?php
$cheks = implode("','", $_POST['checkbox']);
$sql = "delete from $tbl_name where course_code in ('$cheks')";
$result = mysql_query($sql) or die(mysql_error());
mysql_close();
?>
注意: 由于 mysql_ 将在未来弃用,最好使用 mysqli 扩展。但在使用之前,您必须在您的服务器上启用它。 mysqli 是 php 的一部分,较新版本的 php 有它但未启用。要启用此功能,请查看 php 信息页面并在该页面的“加载的配置文件”行中找到 php.ini 文件的路径。 您可以通过在浏览器中加载以下 php 文件来查看 php 信息页面:
<?php
phpinfo();
?>
在文本编辑器中打开该 php.ini 文件并取消注释或在扩展名列表中添加一行 extension=php_mysqli.dll
。
还搜索“extension_dir”并打开它说的目录并确保 php_mysqli.dll 文件在那里。
(如果你不使用 Windows 操作系统,你可能有 .so 扩展名)
然后重启你的服务器就完成了!
Fred -ii-
使用 mysqli_ 和 prepared statements 确实是一个更好的和 更安全的方法。但是,有些人甚至会建议PDO,但即使是PDO 没有 mysqli_ 提供的一些功能; 奇怪的是。即使是 PDO 也需要消毒。许多人认为使用 PDO 可以解决注入问题,这是错误的。 -谢谢弗雷德。
【讨论】:
和 1+ 不使用循环 :) 简洁大方,我的内码猴欣喜若狂。mysql_
弃用通知可能会产生更多“奖励”点,以及 SQL 和 XSS 注入。 ;-)
将mysqli_
与prepared statements 一起使用确实是一种更好更安全的方法。然而,有些人甚至会建议PDO,但即使是PDO也没有mysqli_
提供的一些功能; 奇怪的是。即使是 PDO 也需要消毒。许多人认为使用 PDO 可以解决注入问题,这是错误的。许多人要么不赞成或反对一个好的答案,当不提及mysql_
弃用通知时,以为你想知道;-)
@Fred-ii- 感谢您介绍 PDO 与 mysqli 扩展的比较。我更新了答案。以上是关于如何使用 PHP 使用复选框从 mysql 数据库中删除多行?的主要内容,如果未能解决你的问题,请参考以下文章
复选框状态更改时如何更新mysql字段?使用 jquery (ajax)、php 和 mysql
PHP - 使用 MySQL 数据库中的记录作为值创建复选框
如何使用复选框在 mySQL 中输入 true (1) 或 false (0) 并显示为在 php / html 表单中选中?