mysql表添加复选框删除添加,如何使用PHP从MySQL数据库中删除带有复选框的多行?...

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

This is my top part

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

?>

This is my checkbox code

while($rows=mysql_fetch_array($result)){

?>

<?php echo $rows['course_code']; ?><?php echo $rows['course_name']; ?><?php echo $rows['lecture_id']; ?>

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

}

?>

and, this is my button code

This is my php function code

if(isset($_POST['delete'])){

for($i=0;$i

$del_id = $checkbox[$i];

$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";

$result = mysql_query($sql);

}

if($result){

echo "";

}

}

mysql_close();

?>

解决方案

include all the input elements within your

tags: all inputs are here

update:

to (id doesn't matter here):

and your button code:

to

set opening

tag to

Note:

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

your delete.php file:

$cheks = implode("','", $_POST['checkbox']);

$sql = "delete from $tbl_name where course_code in ('$cheks')";

$result = mysql_query($sql) or die(mysql_error());

mysql_close();

?>

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:

phpinfo();

?>

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!

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值