การอัพเดทหลายแถวผ่าน checkbox ด้วย php Update multiple rows in MySQL with checkboxes
หน้าแรก PHP MySQL เกร็ดความรู้ การอัพเดทหลายแถวผ่าน checkbox ด้วย php Update multiple rows in MySQL with checkboxes
A useful method to select all checkboxes and the ability to update MySQL records with PHP.
First we create a MySQL database, next we create the update page with the checkboxes and add the PHP code to make the update possible.
Simple, easy and fast...
Enjoy!
----------------------------------------------------------------
Create the MySQL database and table.
----------------------------------------------------------------
| Code |
| CREATE DATABASE `test`; USE `test`; CREATE TABLE `test_mysql` ( `id` int(4) NOT NULL auto_increment, `name` varchar(65) NOT NULL default '', `lastname` varchar(65) NOT NULL default '', `email` varchar(65) NOT NULL default '', `status` varchar(1) NOT NULL default '', PRIMARY KEY (`id`) ) type="MyISAM" auto_increment="7" ; -- -- Dumping data for table `test_mysql` -- INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Bob', 'bb@example.com','Y'); INSERT INTO `test_mysql` VALUES (2, 'James', 'Bell', 'jb@example.com','Y'); INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mj@example.com','N'); INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travorse', 'lt@example.com','Y'); INSERT INTO `test_mysql` VALUES (5, 'Joy', 'Ford', 'jf@example.com','Y'); INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gib', 'sgib@example.com','N'); |
----------------------------------------------------------------
multiple_update.php
----------------------------------------------------------------
| Code |
| <?php // Connection to the database $host="localhost"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // 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"); if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox']; if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"]) $id = "('" . implode( "','", $checkbox ) . "');" ; $sql="UPDATE test_mysql SET status = '".(isset($activate)?'Y':'N')."' WHERE id IN $id" ; $result = mysql_query($sql) or die(mysql_error()); } $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset="utf-8"" /> <title>Update multiple rows in mysql with checkbox</title> <script type="text/javascript"> <!-- function un_check(){ for (var i = 0; i < document.frmactive.elements.length; i++) { var e = document.frmactive.elements[i]; if ((e.name != 'allbox') && (e.type == 'checkbox')) { e.checked = document.frmactive.allbox.checked; }}} //--> </script> </head> <body> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="frmactive" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1"> <tr> <td colspan="5"><input name="activate" type="submit" id="activate" value="Activate" /> <input name="deactivate" type="submit" id="deactivate" value="Deactivate" /></td> </tr> <tr> <td> </td> <td colspan="4"><strong>Update multiple rows in mysql with checkbox</strong> </td> </tr><tr> <td align="center"><input type="checkbox" name="allbox" title="Select or Deselct ALL" style="background-color:#ccc;"/></td> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Firstname</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Status</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td><? echo $rows['id']; ?></td> <td><? echo $rows['name']; ?></td> <td><? echo $rows['lastname']; ?></td> <td><? echo $rows['status']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center"> </td> </tr> </table> </form> </td> </tr> </table> </body> </html> |
Enjoy!
refer: http://www.dmxzone.com/go/17550/update-multiple-rows-in-mysql-with-checkboxes/
ขึ้นไปด้านบน
