Ver Mensaje Individual
  #12 (permalink)  
Antiguo 23/12/2011, 06:57
Avatar de loki_gz
loki_gz
 
Fecha de Ingreso: febrero-2008
Mensajes: 48
Antigüedad: 16 años, 2 meses
Puntos: 2
Respuesta: Problema insertando datos desde un array con checkboxes en MySQL

Bueno, pues por fin he dado en el clavo y he conseguido solucionarlo. El tema está en utilizar los corchetes, pero asignándole un número de serie consecutivo que luego evaluaría el loop cuando inserta (o actualiza) los registros.

Según el código de muestra que colgué al principio y con las modificaciones que he hecho, la cosa quedaría así:

prueba.php
Código PHP:
<?php
$editFormAction 
$_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  for(
$key=0;$key<sizeof($_POST['input']);$key++){
  
$insertSQL sprintf("INSERT INTO tabla (input, check) VALUES (%s, %s)",
                       
GetSQLValueString($_POST['input'][$key], "int"),
                       
GetSQLValueString(isset($_POST['check'][$key]) ? "true" """defined","1","0"));

  
mysql_select_db($database_db$db);
  
$Result1 mysql_query($insertSQL$db) or die(mysql_error());
}
}
?>
form.html
Código HTML:
<html>
<body>

<form  id="form" name="form" method="post" action="prueba.php"
<table>
    <tr>
        <td>input 1</td>
        <td>
            <input type="text" name="input[]" value="" />
        </td>
        <td align="center" valign="middle">
<!-- Al nombre de cada check le asigno un número de serie correlativo comenzando por 0" -->
            <input name="check[0]" type="checkbox" value="1"/>
        </td>
    </tr>
    <tr>
        <td>input 2</td>
        <td>
            <input type="text" name="input[]" value="" />
        </td>
        <td align="center" valign="middle">
            <input name="check[1]" type="checkbox" value="1"/>
        </td>
    </tr>
    <tr>
        <td>input 3</td>
        <td>
            <input type="text" name="input[]" value="" />
        </td>
        <td align="center" valign="middle">
            <input name="check[2]" type="checkbox" value="1"/>
        </td>
    </tr>
    <tr>
        <input type="hidden" name="MM_insert" value="form" />
    <input type="submit" value="Insertar" />
    </tr>
</table>
</form>

</body>
</html> 
Salvo que me haya equivocado a la hora de copiar y pegar código, la cosa funciona, ¡probado!

Gracias por vuestra ayuda