在PHP中怎么做批量删除?求代码 php 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 吧sql语句echo 出来,看看到底执行的是什么,不行在想其他的解决办法 这个问题很容易解决,把$_POST["chk"]打印,把SQL语句打印 问题就解决了 “这种方法怎么删除也不行,还有有时候点取消也能删除”貌似是javascript的问题 $_POST["chk"] 的内容是什么? 打印出$_POST["chk"]数据打印出sql语句,就可以知道问题出在哪里了。 $sid里的内容是 各个$_Post['chk']的值合并的字符串, sql里的 in(''.$sid.'')应该不成立吧,把$sid=implode这个函数去掉 你的 checkbox 是什么样子的?注意,是chk[] ,有方括号。<input type='checkbox' name='chk[]' id="chk[]" value=1><p><input type='checkbox' name='chk[]' id="chk[]" value=2><p> 我用的就是chk[]可是没有用 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link type="text/css" rel="stylesheet" href="../style/list.css" /> </head> <?php require_once '../include/conn.php'; //导入分页文件 fenye.php require_once '../include/fenye.php'; //导入处理文件 process.php //require_once '../include/process.php'; ?> <?php //如果不加这句会报错 ini_set("error_reporting","E_ALL & ~E_NOTICE"); //1.单个删除 //获取对应的id进行删除 $newId = $_REQUEST["id"]; $flag = $_REQUEST["flag"]; if(isset($flag)){ if($flag==1){ $del = "delete from news where news_id ='$newId'"; $result = mysql_query($del,$conn); if($result){ echo "<script>alert('删除成功');window.location.href='admin.php?sel=2';</script>"; }else{ echo "<script>alert('删除失败');history.back;</script>"; } } } //2.批量删除 if(isset($_POST["chk"])){ $ids = implode(",",$_POST["chk"]); $query = mysql_query("delete from news where news_id in ($ids)"); if($query){ echo "<script>alert(\"删除成功\");window.location.href='admin.php?sel=2';</script>"; } else { echo "<script>alert(\"删除失败\");history.back;</script>"; } } //3.获取下拉选的值 $selValue = $_REQUEST['s1']; ?> <body> <!-- 外面的大层 --> <form action="admin.php?sel=2" method="post" name="form1" > <div class="divOut"> <!-- 第一行 --> <div id="divLine11"> <div id="div011" style="padding-top:2px;"> <a class="ext1" href="admin.php?sel=21"><img src="../img/insert.png" style="width:14px; height:14px; margin-top:1px;"/>添加文章</a> </div> <div id="div011"><input type="submit" name="selAll" value="全部选中" onclick="All()"/></div> <div id="div011"><input type="submit" name="reAll" value="全部取消" onclick="reAll()"/></div> <div id="div011"> <input type="submit" name="delAll" value="删除选中项" onclick="del()"/> </div> <div id="div011"> <select name="s1" id="s2" onchange="selValue()"> <option value="0">请选择</option> <option value="1">全部</option> <option value="2">站内新闻</option> <option value="3">建站常识</option> </select> </div> <div id="div011"> <input type="text" name="s2" /> </div> <div id="div011"> <input type="image" src="../img/search.png" name="s3" style="padding-left:80px; padding-top:5px; width:16px;height:16px;"/> </div> </div> <!-- 动态输出表格 --> <div class="tabOut"> <table id="tab" cellspacing="0"> <tr id="t1"> <th id="th1">操作</th> <th id="th1">标题</th> <th id="th1">作者</th> <th id="th1">分类</th> <th id="th1">发布时间</th> <th id="th1">编辑</th> <th id="th1">删除</th> </tr> <tr> <?php if(isset($selValue)){ if($selValue==1){ $sql="select * from news where news_class ='$selValue'"; //执行语句一 }else if($selValue==2){ $sql="select * from news where news_class ='$selValue'"; //执行语句2 }else if($selValue==3){ //$sql="select * from news where news_class ='$selValue'"; $sql="select * from news limit ".($pageNow-1)*$pageSize.",".$pageSize.""; //执行语句3 }else if($selValue==0){ $sql="select * from news where news_class ='$selValue'"; } } $rs = mysql_query($sql,$conn); while (false!=($res=mysql_fetch_assoc($rs))){ ?> <?php echo "<td id='th2'><input type='checkbox' name='chk[]' id='{$res['news_id']}'/></td>"; echo "<td id='th2'>{$res['news_title']}</td>"; echo "<td id='th2'>{$res['news_author']}</td>"; echo "<td id='th2'>{$res['news_class']}</td>"; echo "<td id='th2'>{$res['news_time']}</td>"; echo "<td id='th2'><a href='admin.php?sel=22'><img src='../img/edit.gif'/></a></td>"; echo "<td id='th2'> <a href=\"javascript:if (confirm('真的要删除吗?'))location.href='admin.php?sel=2&flag=1&id={$res['news_id']}'\"> <img src='../img/del.png'/> </a> </td>"; ?> </tr> <?php } ?> </table> </div> <!-- 分页 --> <div id="page"> <?php require_once '../include/page.php';?> </div> </div> </form> </body></html><script type="text/javascript" src="../js/news.js"></script>//下面是javascript代码function All(){ var all_check = document.getElementsByName("chk[]"); for(var i=0;i<all_check.length;i++){ var checks = all_check[i]; if(checks.checked==false){ checks.checked = true; } }}function reAll(){ var all_check = document.getElementsByName("chk[]"); for(var i=0;i<all_check.length;i++){ var checks = all_check[i]; if(checks.checked==true){ checks.checked = false; } }}function del(){ var all_check = document.getElementsByName("chk[]"); var num=0; for(var i=0;i<all_check.length;i++){ var checks = all_check[i]; if(checks.checked){ num++; } } if(num==0){ alert("请选择!"); } else{ var txt="确定要删除这"+num+"个吗?"; if(window.confirm(txt)==true){ return true; } else{ return false; } }} echo "<td id='th2'><input type='checkbox' name='chk[]' id='{$res['news_id']}' value='{$res['news_id']}' /></td>";你的checkbox没有给value值。 点取消也能删除是因为这句要这么写:<input type="submit" name="delAll" value="删除选中项" onclick="return del()"/>还有del() 函数里面:if(num==0){alert("请选择!");return false;} 本人要做个家教网,请大家帮忙给我估算一下价格 求助,值的累加问题 想利用2.php对1.php进行过滤,请问如何编写2.php 急急!!!在线等待。。。(php中url传递参数?) 新手到来,问:怎样用PHP调用SQLServer的存储过程 求救!我们网站的问题到底出在哪? Adodb初学问题 查询PHP函数 简单问题,通过odbc连上oracle后,用odbc函数能否查到数据? 我想学PHP,各位有什么好的网址或者PHP电子书的网址介绍给小弟吗? 请教一个历史同期数据对比的问题 怎么判断里面的内容执行某些操作?
貌似是javascript的问题
打印出sql语句,就可以知道问题出在哪里了。
<input type='checkbox' name='chk[]' id="chk[]" value=1><p>
<input type='checkbox' name='chk[]' id="chk[]" value=2><p>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="../style/list.css" />
</head>
<?php
require_once '../include/conn.php';
//导入分页文件 fenye.php
require_once '../include/fenye.php';
//导入处理文件 process.php
//require_once '../include/process.php';
?>
<?php
//如果不加这句会报错
ini_set("error_reporting","E_ALL & ~E_NOTICE");
//1.单个删除
//获取对应的id进行删除
$newId = $_REQUEST["id"];
$flag = $_REQUEST["flag"];
if(isset($flag)){
if($flag==1){
$del = "delete from news where news_id ='$newId'";
$result = mysql_query($del,$conn);
if($result){
echo "<script>alert('删除成功');window.location.href='admin.php?sel=2';</script>";
}else{
echo "<script>alert('删除失败');history.back;</script>";
}
}
}
//2.批量删除
if(isset($_POST["chk"])){
$ids = implode(",",$_POST["chk"]);
$query = mysql_query("delete from news where news_id in ($ids)");
if($query){
echo "<script>alert(\"删除成功\");window.location.href='admin.php?sel=2';</script>";
}
else {
echo "<script>alert(\"删除失败\");history.back;</script>";
}
}
//3.获取下拉选的值
$selValue = $_REQUEST['s1'];
?>
<body>
<!-- 外面的大层 -->
<form action="admin.php?sel=2" method="post" name="form1" >
<div class="divOut">
<!-- 第一行 -->
<div id="divLine11">
<div id="div011" style="padding-top:2px;">
<a class="ext1" href="admin.php?sel=21"><img src="../img/insert.png" style="width:14px; height:14px; margin-top:1px;"/>添加文章</a>
</div>
<div id="div011"><input type="submit" name="selAll" value="全部选中" onclick="All()"/></div>
<div id="div011"><input type="submit" name="reAll" value="全部取消" onclick="reAll()"/></div>
<div id="div011">
<input type="submit" name="delAll" value="删除选中项" onclick="del()"/>
</div>
<div id="div011">
<select name="s1" id="s2" onchange="selValue()">
<option value="0">请选择</option>
<option value="1">全部</option>
<option value="2">站内新闻</option>
<option value="3">建站常识</option>
</select>
</div>
<div id="div011">
<input type="text" name="s2" />
</div>
<div id="div011">
<input type="image" src="../img/search.png" name="s3" style="padding-left:80px; padding-top:5px; width:16px;height:16px;"/>
</div>
</div>
<!-- 动态输出表格 -->
<div class="tabOut">
<table id="tab" cellspacing="0">
<tr id="t1">
<th id="th1">操作</th>
<th id="th1">标题</th>
<th id="th1">作者</th>
<th id="th1">分类</th>
<th id="th1">发布时间</th>
<th id="th1">编辑</th>
<th id="th1">删除</th>
</tr>
<tr>
<?php
if(isset($selValue)){
if($selValue==1){
$sql="select * from news where news_class ='$selValue'";
//执行语句一
}else if($selValue==2){
$sql="select * from news where news_class ='$selValue'";
//执行语句2
}else if($selValue==3){
//$sql="select * from news where news_class ='$selValue'";
$sql="select * from news limit ".($pageNow-1)*$pageSize.",".$pageSize."";
//执行语句3
}else if($selValue==0){
$sql="select * from news where news_class ='$selValue'";
}
}
$rs = mysql_query($sql,$conn);
while (false!=($res=mysql_fetch_assoc($rs))){
?>
<?php
echo "<td id='th2'><input type='checkbox' name='chk[]' id='{$res['news_id']}'/></td>";
echo "<td id='th2'>{$res['news_title']}</td>";
echo "<td id='th2'>{$res['news_author']}</td>";
echo "<td id='th2'>{$res['news_class']}</td>";
echo "<td id='th2'>{$res['news_time']}</td>";
echo "<td id='th2'><a href='admin.php?sel=22'><img src='../img/edit.gif'/></a></td>";
echo "<td id='th2'>
<a href=\"javascript:if (confirm('真的要删除吗?'))location.href='admin.php?sel=2&flag=1&id={$res['news_id']}'\">
<img src='../img/del.png'/>
</a>
</td>";
?>
</tr>
<?php
}
?>
</table>
</div>
<!-- 分页 -->
<div id="page">
<?php require_once '../include/page.php';?>
</div>
</div>
</form>
</body>
</html>
<script type="text/javascript" src="../js/news.js">
</script>
//下面是javascript代码function All()
{
var all_check = document.getElementsByName("chk[]");
for(var i=0;i<all_check.length;i++){
var checks = all_check[i];
if(checks.checked==false){
checks.checked = true;
}
}
}function reAll()
{
var all_check = document.getElementsByName("chk[]");
for(var i=0;i<all_check.length;i++){
var checks = all_check[i];
if(checks.checked==true){
checks.checked = false;
}
}
}
function del(){
var all_check = document.getElementsByName("chk[]");
var num=0;
for(var i=0;i<all_check.length;i++){
var checks = all_check[i];
if(checks.checked){
num++;
}
}
if(num==0){
alert("请选择!");
}
else{
var txt="确定要删除这"+num+"个吗?";
if(window.confirm(txt)==true){
return true;
}
else{
return false;
}
}
}
<input type="submit" name="delAll" value="删除选中项" onclick="return del()"/>还有del() 函数里面:
if(num==0){
alert("请选择!");
return false;
}