原生PHP列表选项批量删除并同时删除上传图片

<!-- 加个form标签,用于提交删除所用 -->
<form method="post" action="本页.php">  
  <label>  
    <input type="checkbox" name="id[]" value="<?php echo $rs['id'];?>" />  
  </label>   
  <div>
    <input type="button" value="全选" onClick="selectBox('all')" />   
    <input type="button" value="反选" onClick="selectBox('reverse')" />   
    <input type="submit" name="btnSave" value="删除" />                          
  </div>   
</form>  
<script type="text/javascript">  
    function selectBox(selectType){								//全选、反选
        var checkboxis = document.getElementsByName("id[]");  
        if(selectType == "reverse"){							//反选 
            for (var i=0; i<checkboxis.length; i++){  
                //alert(checkboxis[i].checked);  
                checkboxis[i].checked = !checkboxis[i].checked;  
            }  
        }else if(selectType == "all"){							//全选
            for (var i=0; i<checkboxis.length; i++){  
                //alert(checkboxis[i].checked);  
                checkboxis[i].checked = true;  
            }  
        }  
    }  
</script>
<?php  
  header('Content-Type: text/html; charset=utf-8'); 
  $mysql_server_name='localhost'; 						//服务器
  $mysql_username='username'; 							//用户名
  $mysql_password='password'; 							//数据库密码
  $mysql_database='dbname'; 							//数据库名
  $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; 			//连接数据库
  mysql_query("set names 'utf8'"); 						//数据库输出编码.
  mysql_select_db($mysql_database); 					//打开数据库 
  //删除选定项
  if(@$_POST['btnSave']){								//@防报错btnSave未定义  
    if(empty($_POST['id'])){  
      echo"<script>alert('必须选择一个产品,才可以删除!');history.back(-1);</script>";  
      exit;  
    }else{  
      //删除上传图片
      $arr=$_POST['id'];
      $len=count($arr);
      for($i=0;$i<$len;$i++){
        $info ="select * from product where id=".$arr[$i];
        $aa=mysql_query($info);
        $bb = mysql_fetch_assoc($aa);					//变成数组才可打印查看
        $tu=$bb['pic'];
        unlink($tu);
      }
      /*如果要获取全部数值则使用下面代码*/ 
      $id= implode(",",$_POST['id']);
      //下方的 ` 符号通常用于MySQL中说明其中的内容是数据库名、表名、字段名等,避免和MySQL本身的关键字冲突。  
      $str="DELETE FROM `product` where id in ($id)";
      mysql_query($str);  
      echo "<script>alert('删除成功!');window.location.href='本页.php';</script>";  
    }  
  } 

猜你喜欢

转载自blog.csdn.net/qq_38882327/article/details/88988119