Php+js Implementing the bulk delete Data feature

Source: Internet
Author: User
Tags pconnect php database
This article mainly introduced the Php+js realizes the bulk deletion data function, combined with the instance form analysis PHP unifies the JS control page element The selection and the submission, as well as PHP operation MySQL realizes the bulk deletion function the related realization technique, at the end also comes with a PHP database operation class, needs the friend can refer to, Hope to help everyone.

The example in this paper describes the PHP+JS implementation of bulk delete data function. Share to everyone for your reference, as follows:

Form

<form id= "Form2" name= "Form2" method= "post" action= "del_product.php" onsubmit= "return Checkf (This)" ><label ><input type= "checkbox" Name= "id[]" value= "<?php echo $rs [' id '];? > "style=" Background:none; Border:none; "/></label><p style=" padding-left:20px; ><input type= "button" value= "Select All" style= "Background:url (images/cheall.jpg) no-repeat; width:60px; height:23px; Border:none; "onclick=" Selectbox (' All ') '/><input type= ' button ' value= ' reverse select ' style= ' Background:url (images/ cheall.jpg) No-repeat; width:60px; height:23px; Border:none, "onclick=" selectbox (' reverse ') "/><input type=" Submit "Name=" Btnsave "style=" Background:url ( images/cheall.jpg) No-repeat; width:60px; height:23px; Border:none; "value=" Delete "/></p></form>

Js

<script type= "Text/javascript" language= "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>

del_product.php

<?phpinclude (' checkadmin.php '); header (' content-type:text/html; Charset=utf-8 '); if ($_post[' btnsave ']) {if ( Empty ($_post[' id ')) {    echo "<script>alert (' must select a product before it can be deleted! '); History.back ( -1);</script> ";    Exit;  } else{/* If you want to get all the values, use the following code *   /$id = Implode (",", $_post[' id ']);   $str = "DELETE from ' product ' where ID in ($id)";   mysql_query ($STR);  echo "<script>alert (' Delete succeeded! '); window.location.href= ' product_list.php ';</script> ";}}? >

Attached: PHP Implementation of the database Operation class

db.php:

<?phpclass DB {private $link _id;  Private $handle;  Private $is _log;  Private $time;    Constructor public Function __construct () {$this->time = $this->microtime_float ();    Require_once ("config.db.php"); $this->connect ($db _config["hostname"], $db _config["username"], $db _config["password", $db _config["database"],    $db _config["Pconnect"]);    $this->is_log = $db _config["Log"]; if ($this->is_log) {$handle = fopen ($db _config["LogFilePath"]. "      Dblog.txt "," A + ");    $this->handle= $handle; }}//Database connection Public function connect ($dbhost, $dbuser, $DBPW, $dbname, $pconnect = 0, $charset = ' utf8 ') {if ($pconnect      ==0) {$this->link_id = @mysql_connect ($dbhost, $dbuser, $DBPW, true);      if (! $this->link_id) {$this->halt ("Database connection failed");      }} else {$this->link_id = @mysql_pconnect ($dbhost, $dbuser, $DBPW);      if (! $this->link_id) {$this->halt ("Database Persistent connection failed"); }} if (! @mysql_select_db ($dbname, $this->link_id) {$this->halt (' Database selection failed ');  } @mysql_query ("Set names". $charset);    }//Query Public Function query ($sql) {$this->write_log ("Query". $sql);    $query = mysql_query ($sql, $this->link_id);    if (! $query) $this->halt (' Query Error: '. $sql);  return $query; }//Get a record (Mysql_assoc,mysql_num,mysql_both) public function Get_one ($sql, $result _type = mysql_assoc) {$query = $thi    S->query ($sql);    $rt =& mysql_fetch_array ($query, $result _type);    $this->write_log ("Get a Record". $sql);  return $rt;    }//Get all records public function Get_all ($sql, $result _type = mysql_assoc) {$query = $this->query ($sql);    $i = 0;    $rt = Array ();      while ($row =& mysql_fetch_array ($query, $result _type)) {$rt [$i]= $row;    $i + +;    } $this->write_log ("Get All Records". $sql);  return $rt;    }//Insert Public Function Insert ($table, $dataArray) {$field = "";    $value = "";  if (!is_array ($dataArray) | | count ($dataArray) <=0) {$this->halt (' No data to insert ');    return false;      } while (List ($key, $val) =each ($dataArray)) {$field. = "$key,";    $value. = "' $val ',";    } $field = substr ($field, 0,-1);    $value = substr ($value, 0,-1);    $sql = "INSERT into $table ($field) VALUES ($value)";    $this->write_log ("Insert". $sql);    if (! $this->query ($sql)) return false;  return true; }//Update public Function Update ($table, $dataArray, $condition = "") {if (!is_array ($dataArray) | | count ($dataArray) <=      0) {$this->halt (' No data to be updated ');    return false;    } $value = "";    while (list ($key, $val) = each ($dataArray)) $value. = "$key = ' $val ',";    $value. = substr ($value, 0,-1);    $sql = "Update $table set $value where 1=1 and $condition";    $this->write_log ("Update". $sql);    if (! $this->query ($sql)) return false;  return true;      }//Delete public Function Delete ($table, $condition = "") {if (empty ($condition)) {$this->halt (' No conditions set for deletion ');    return false; } $sql = "Delete from $table where 1=1 and $condition ";    $this->write_log ("delete". $sql);    if (! $this->query ($sql)) return false;  return true;    }//Returns the result set Public function Fetch_array ($query, $result _type = Mysql_assoc) {$this->write_log ("return result set");  Return mysql_fetch_array ($query, $result _type);      }//Gets the number of record bars public function num_rows ($results) {if (!is_bool ($results)) {$num = Mysql_num_rows ($results);      $this->write_log ("Gets the number of record bars". $num);    return $num;    } else {return 0;    }}//release result set public function Free_result () {$void = Func_get_args (); foreach ($void as $query) {if (Is_resource ($query) && get_resource_type ($query) = = = ' MySQL result ') {R      Eturn mysql_free_result ($query);  }} $this->write_log ("release result set");    }//Gets the last inserted ID public function insert_id () {$id = mysql_insert_id ($this->link_id);    $this->write_log ("Last inserted ID is". $id);  return $id;    }//Close database connection protected function close () {$this->write_log ("database connection closed"); Return @mysqL_close ($this->link_id);    }//Error hint Private function halt ($msg = ') {$msg. = "\ r \ n". Mysql_error ();    $this->write_log ($msg);  Die ($msg);    }//destructor public Function __destruct () {$this->free_result ();    $use _time = ($this, microtime_float ())-($this->time);    $this->write_log ("completes the entire query task, takes time". $use _time);    if ($this->is_log) {fclose ($this->handle); }}//write log file Public function write_log ($msg = ') {if ($this->is_log) {$text = Date ("y-m-d h:i:s"). " ". $msg."      \ r \ n ";    Fwrite ($this->handle, $text);    }}//Gets the number of milliseconds public function microtime_float () {list ($usec, $sec) = Explode ("", Microtime ());  return (float) $usec + (float) $sec); }}?>

config.db.php

<?php  $db _config["hostname"] = "localhost";//server address  $db _config["username"] = "root";//database user name  $db _ config["password"] = "123"; Database Password  $db _config["database"] = "test";//Database name  $db _config["charset"] = "UTF8";//Database encoding  $db _config[" Pconnect "] = 1;//Open persistent connection  $db _config[" log "] = 1;//open log  $db _config[" logfilepath "] = './';//Open log?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.