Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/12/2012, 20:56
ruben_chirinos_1985
Invitado
 
Mensajes: n/a
Puntos:
modificar varios registros a la vez

Alguien que me pueda ayudar por favor, necesito que de la consulta de la tabla que se me genera, pueda al mismo tiempo introduicr cambios en los campos y seleccionar con checkbox y que al enviar se actualice todo de una vez. La idea es para actualizar las filas que han sido seleccionadas con el checkbox. Estoy tratando de adaptar el siguiente script.

Código PHP:
Ver original
  1. <?php
  2.     // Connection to the database
  3.     $host="localhost"; // Host name
  4.     $username="root"; // Mysql username
  5.     $password=""; // Mysql password
  6.     $db_name="empleados"; // Database name
  7.     $tbl_name="empleados"; // Table name
  8.      
  9.     // Connect to server and select databse.
  10.     mysql_connect("$host", "$username", "$password")or die("cannot connect");
  11.     mysql_select_db("$db_name")or die("cannot select DB");
  12.     $nombre=$_POST['bv'];
  13.     if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];
  14.      
  15.      
  16.     if(isset($_POST['bv']) && isset($_POST['activate'])?
  17.      
  18.     $activate = $_POST["activate"]:
  19.     $deactivate = $_POST["deactivate"])
  20.      
  21.     $id = "('" . implode("','", $checkbox ) . "');" ;
  22.      
  23.      
  24.     $sql="UPDATE empleados SET  nombre = '".$nombre."', status = '".(isset($activate)?'Y':'N')."' WHERE id IN $id" ;
  25.      
  26.     $result = mysql_query($sql) or die(mysql_error());
  27.     }
  28.      
  29.     $sql="SELECT * FROM $tbl_name";
  30.     $result=mysql_query($sql);
  31.      
  32.     $count=mysql_num_rows($result);
  33.     ?>
  34.      
  35.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  36.     <html xmlns=" http://www.w3.org/1999/xhtml ">
  37.     <head>
  38.      
  39.     <meta http-equiv="Content-Type" content="text/html; charset="utf-8"" />
  40.     <title>Update multiple rows in mysql with checkbox</title>
  41.      
  42.     <script type="text/javascript">
  43.     <!--
  44.     function un_check(){
  45.     for (var i = 0; i < document.frmactive.elements.length; i++) {
  46.     var e = document.frmactive.elements[i];
  47.     if ((e.name != 'allbox') && (e.type == 'checkbox')) {
  48.     e.checked = document.frmactive.allbox.checked;
  49.     }}}
  50.     //-->
  51.     </script>
  52.      
  53.     </head>
  54.     <body>
  55.      
  56.     <p>&nbsp;</p>
  57.     <table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
  58.     <tr>
  59.     <td><form name="frmactive" method="post" action="">
  60.     <table width="400" border="1" cellpadding="3" cellspacing="0" bordercolor="#996633">
  61.     <tr>
  62.     <td colspan="6" bgcolor="#8BC0FF"><input name="activate" type="submit" id="activate" value="Activate" />
  63.     <input name="deactivate" type="submit" id="deactivate" value="Deactivate" /></td>
  64.     </tr>
  65.     <tr>
  66.     <td colspan="6" bgcolor="#7895B0"><div align="center"><strong>Update multiple rows in mysql with checkbox</strong> </div></td>
  67.     </tr><tr>
  68.     <td align="center"><input type="checkbox" name="allbox" onchange="un_check()" title="Select or Deselct ALL" style="background-color:#ccc;"/></td>
  69.     <td align="center" bgcolor="#ECE9D8"><strong>Id</strong></td>
  70.     <td align="center" bgcolor="#ECE9D8"><strong>Firstname</strong></td>
  71.     <td align="center" bgcolor="#ECE9D8"><strong>Lastname</strong></td>
  72.     <td align="center" bgcolor="#ECE9D8"><strong>Status</strong></td>
  73.     </tr>
  74.     <?php
  75.     while($rows=mysql_fetch_array($result)){
  76.     ?>
  77.     <tr>
  78.     <td align="center" bgcolor="#C5D5FC"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php  echo $rows['id']; ?>"></td>
  79.     <td bgcolor="#C5D5FC"><?php  echo $rows['id']; ?></td>
  80.     <td bgcolor="#C5D5FC"><input name="bv" type="text" id="bv" value="<?php  echo $rows['nombre']; ?>" size="5" maxlength="4" /></td>
  81.     <td bgcolor="#C5D5FC"><?php  echo $rows['apellido']; ?></td>
  82.     <td bgcolor="#C5D5FC"><?php  echo $rows['status']; ?></td>
  83.     </tr>
  84.     <?php
  85.     }
  86.     ?>
  87.     <tr>
  88.     <td colspan="6" align="center">&nbsp;</td>
  89.     </tr>
  90.     </table>
  91.     </form>
  92.     </td>
  93.     </tr>
  94.     </table>
  95.     </body>
  96.     </html>

y esta es la bd empleados

Código SQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `empleados` (
  2.   `id` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `nombre` VARCHAR(35) NOT NULL,
  4.   `apellido` VARCHAR(35) NOT NULL,
  5.   `status` VARCHAR(5) NOT NULL,
  6.   PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
  8.  
  9. --
  10. -- Volcar la base de datos para la tabla `empleados`
  11. --
  12.  
  13. INSERT INTO `empleados` (`id`, `nombre`, `apellido`, `status`) VALUES
  14. (1, 'u', 'erfe', 'N'),
  15. (2, 'u', 'ferf', 'N'),
  16. (3, 'u', 'erfe', 'Y'),
  17. (4, 'u', 'ferf', 'Y'),