PHP删除多行与复选框 [英] PHP Delete Multiple Rows With Check Box
本文介绍了PHP删除多行与复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用复选框删除多行。但值不是删除。
我不知道我在哪里错了。
$ b
<?php
$ host =localhost; // Host name
$ username =DB_USERNAME; // Mysql username
$ password =DB_PASSWORD; // Mysql password
$ db_name =DB_NAME; //数据库名称
$ tbl_name =TABLE_NAME; //表名
//连接到服务器并选择databse。
mysql_connect($ host,$ username,$ password)或die(can not connect);
mysql_select_db($ db_name)或die(can not select DB);
?>
exe.php
<?php
includeconnect.php;
//检查是否删除按钮处于活动状态,启动此
if($ _ POST ['delete'])
{
$ id = $ _POST [ ];
$ count = count($ id);
for($ i = 0; $ i< $ count; $ i ++){
// echo< br> value =。$ id [$ i]。Jumlah =。$ count;
$ sql =DELETE FROM $ tbl_name WHERE id ='$ id [$ i]';
$ result = mysql_query($ sql);
}
//如果成功重定向到delete_multiple.php
if($ result){echo< meta http-equiv = \refresh\content = \0; URL = index.php\>;}
}
mysql_close();
?>
index.php
<?php
includeconnect.php;
$ result = mysql_query(SELECT * FROM $ tbl_name ORDER BY ts DESC);
?>
< table width =400border =0cellspacing =1cellpadding =0>
< tr>
< td>< form name =form1method =postaction =exe.php>
< table width =400border =0cellpadding =3cellspacing =1bgcolor =#CCCCCC>
< tr>
< td bgcolor =#FFFFFF> < / td>
< td colspan =4bgcolor =#FFFFFF>< strong>在mysql中删除多行< / strong> < / td>
< / tr>
< tr>
< td align =centerbgcolor =#FFFFFF>#< / td>
< td align =centerbgcolor =#FFFFFF>< strong>订单ID< / strong>< / td>
< td align =centerbgcolor =#FFFFFF>< strong>用户名< / strong>< / td>
< td align =centerbgcolor =#FFFFFF>< strong>链接< / strong>< / td>
< td align =centerbgcolor =#FFFFFF>< strong> Type< / strong>< / td>
< / tr>
<?php
while($ rows = mysql_fetch_array($ result)){
?
< tr>
< td align =centerbgcolor =#FFFFFF>< input name =data []type =checkboxid =datavalue =<?php echo $ rows ['oid'];?>>
< / td>
< td bgcolor =#FFFFFF><?php echo $ rows ['oid']; ?>< / td>
< td bgcolor =#FFFFFF><?php echo $ rows ['user']; ?>< / td>
< td bgcolor =#FFFFFF><?php echo $ rows ['data']; ?>< / td>
< td bgcolor =#FFFFFF><?php echo $ rows ['type']; ?>< / td>
< / tr>
<?php
} unset($ rows);
?>
< tr>
< td colspan =5align =centerbgcolor =#FFFFFF>< input name =deletetype =submitid =deletevalue =Delete>
< / td>
< / tr>
< / table>
< / form>
< / td>
< / tr>
< / table>
有人请帮我,告诉我我错了什么。
解决方案
发送5个php文件(8个css和js文件)关于这一点,您可以自己使用代码,如果您有任何问题,请发表评论:
此链接还有其.sql文件,因此您只需导入它并开始测试。
I'm Trying To Delete Multiple Rows By Using Check Boxes. But The Values Are Not Deleting. I Don't Understand Where I'm Going Wrong. Please Help Me.
Connect.php
<?php
$host="localhost"; // Host name
$username="DB_USERNAME"; // Mysql username
$password="DB_PASSWORD"; // Mysql password
$db_name="DB_NAME"; // Database name
$tbl_name="TABLE_NAME"; // 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");
?>
exe.php
<?php
include "connect.php";
// Check if delete button active, start this
if($_POST['delete'])
{
$id = $_POST['data'];
$count = count($id);
for($i=0;$i<$count;$i++){
//echo "<br> value = ".$id[$i]."Jumlah = ".$count ;
$sql = "DELETE FROM $tbl_name WHERE id='$id[$i]'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}
}
mysql_close();
?>
index.php
<?php
include "connect.php";
$result=mysql_query("SELECT * FROM $tbl_name ORDER BY ts DESC");
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="exe.php">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Order Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Type</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="data[]" type="checkbox" id="data" value="<?php echo $rows['oid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['oid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['user']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['data']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['type']; ?></td>
</tr>
<?php
}unset($rows);
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
Someone please help me with this and tell me where am i wrong. I'm still learning PHP.
解决方案
Ill send 5 php files (8 with the css and js files) about this so you can use the codes yourself and comment if you have questions:
This link also has its .sql file so you just need to import it and start testing.
这篇关于PHP删除多行与复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文