如何使用PHP从复选框中删除MySQL数据库中的多行? [英] How to delete multiple rows from mysql database with checkbox using PHP?

查看:117
本文介绍了如何使用PHP从复选框中删除MySQL数据库中的多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试delete在"admin"数据库中的数据,但是删除"按钮不起作用.

I try to delete my data in "admin" database, but the delete button does not function.

这是我的最重要部分

<?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函数代码

This is my php function code

<?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>标记内包括所有输入元素:<form> all inputs are here </form>

include all the input elements within your <form> tags: <form> all inputs are here </form>

更新:

<input name = "checkbox[]" type="checkbox"  id="checkbox[]" value="<?php echo     $rows['course_code'];?>">

至(此处无关紧要):

<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".

Note: I assume below codes are in delete.php file. if not replace "delete.php" with that name in above opening form tag.

您的delete.php文件:

your delete.php file:

<?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信息页面:

Note: Since mysql_ will deprecate on future, better is use mysqli extension. But before use that, you have to enable it on your server. mysqli is a part of php and newer version of php has it but not enabled. To enable this, view php info page and find the path of php.ini file in "Loaded Configuration File" row on that page. You can see php info page by loading below php file in the browser:

<?php
 phpinfo();
?>

在文本编辑器中打开该php.ini文件,然后取消注释或在扩展列表中添加extension=php_mysqli.dll行. 还搜索"extension_dir"并打开其显示的目录,并确保php_mysqli.dll文件在那里. (如果不使用Windows操作系统,则扩展名为.so)

open that php.ini file in a text editor and un-comment or add a line extension=php_mysqli.dll at the extensions list there. also search for "extension_dir" and open the directory it says and make sure php_mysqli.dll file is there. (you may have .so extension if you not use windows OS)

然后重新启动服务器,您就完成了!

Then restart your server and you are done!

通过 Fred -ii-

将mysqli_与准备好的语句一起使用一个更好的 更安全的方法.但是,有些人甚至会建议 PDO ,甚至是PDO 没有mysqli_提供的某些功能; 奇怪的是.甚至PDO也需要消毒.许多人认为使用PDO可以解决注入问题,这是错误的. -谢谢弗雷德.

Using mysqli_ with prepared statements is indeed a better and safer method. However, some will even suggest PDO, but even PDO doesn't have some of the functionalities that mysqli_ offers; strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issues, which is false. -Thanks Fred.

这篇关于如何使用PHP从复选框中删除MySQL数据库中的多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆