Código PHP:
Ver original
<?php // Connection to the database $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="empleados"; // Database name $tbl_name="empleados"; // Table name // Connect to server and select databse. $nombre=$_POST['bv']; $activate = $_POST["activate"]: $deactivate = $_POST["deactivate"]) $sql="UPDATE empleados SET nombre = '".$nombre."', status = '".(isset($activate)?'Y':'N')."' WHERE id IN $id" ; } $sql="SELECT * FROM $tbl_name"; ?> <!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> <p> </p> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td><form name="frmactive" method="post" action=""> <table width="400" border="1" cellpadding="3" cellspacing="0" bordercolor="#996633"> <tr> <td colspan="6" bgcolor="#8BC0FF"><input name="activate" type="submit" id="activate" value="Activate" /> <input name="deactivate" type="submit" id="deactivate" value="Deactivate" /></td> </tr> <tr> <td colspan="6" bgcolor="#7895B0"><div align="center"><strong>Update multiple rows in mysql with checkbox</strong> </div></td> </tr><tr> <td align="center"><input type="checkbox" name="allbox" onchange="un_check()" title="Select or Deselct ALL" style="background-color:#ccc;"/></td> <td align="center" bgcolor="#ECE9D8"><strong>Id</strong></td> <td align="center" bgcolor="#ECE9D8"><strong>Firstname</strong></td> <td align="center" bgcolor="#ECE9D8"><strong>Lastname</strong></td> <td align="center" bgcolor="#ECE9D8"><strong>Status</strong></td> </tr> <?php ?> <tr> <td align="center" bgcolor="#C5D5FC"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td> <td bgcolor="#C5D5FC"><?php echo $rows['id']; ?></td> <td bgcolor="#C5D5FC"><input name="bv" type="text" id="bv" value="<?php echo $rows['nombre']; ?>" size="5" maxlength="4" /></td> <td bgcolor="#C5D5FC"><?php echo $rows['apellido']; ?></td> <td bgcolor="#C5D5FC"><?php echo $rows['status']; ?></td> </tr> <?php } ?> <tr> <td colspan="6" align="center"> </td> </tr> </table> </form> </td> </tr> </table> </body> </html>
y esta es la bd empleados
Código SQL:
Ver original
CREATE TABLE IF NOT EXISTS `empleados` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `nombre` VARCHAR(35) NOT NULL, `apellido` VARCHAR(35) NOT NULL, `status` VARCHAR(5) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; -- -- Volcar la base de datos para la tabla `empleados` -- INSERT INTO `empleados` (`id`, `nombre`, `apellido`, `status`) VALUES (1, 'u', 'erfe', 'N'), (2, 'u', 'ferf', 'N'), (3, 'u', 'erfe', 'Y'), (4, 'u', 'ferf', 'Y'),