Checkbox

Puedes ayudarme con el código de varias filas de una tabla cada una con checkbox, solo los marcados se guardan en la base de datos.

1 Respuesta

Respuesta
1
Te referencio un mensaje que respondí no hace mucho, en el cual se eliminan los registros marcados en su checkbox:
http://www.todoexpertos.com/herramientas/leermsgpublico.aspx?idproceso=889924
Si tienes alguna duda sobre cómo adaptarlo pregunta sin problemas.
Un saludo,
iNphYx
No puedo utilizar ese código porque mi página no direcciona a otra sino que se supone guarda los registros directamente.
El problema es que uso Dreamweaver y me es difícil poner el código.
Si me ayudas por favor, te envío el código que se genera:
<?php require_once('Connections/dbssantos.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertar")) {
$insertSQL = sprintf("INSERT INTO tblfas (CODFAS, NOMFAS, DURFAS, OBSFAS, CODPRO) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['CODIGO'], "text"),
GetSQLValueString($_POST['NOMBRE'], "text"),
GetSQLValueString($_POST['DURACION'], "double"),
GetSQLValueString($_POST['OBSERVACION'], "text"),
GetSQLValueString($_POST['proyecto'], "text"));
mysql_select_db($database_dbssantos, $dbssantos);
$Result1 = mysql_query($insertSQL, $dbssantos) or die(mysql_error());
}
mysql_select_db($database_dbssantos, $dbssantos);
$query_proyecto = "SELECT CODPRO, NOMPRO FROM tblpro";
$proyecto = mysql_query($query_proyecto, $dbssantos) or die(mysql_error());
$row_proyecto = mysql_fetch_assoc($proyecto);
$totalRows_proyecto = mysql_num_rows($proyecto);
$maxRows_fase = 10;
$pageNum_fase = 0;
if (isset($_GET['pageNum_fase'])) {
$pageNum_fase = $_GET['pageNum_fase'];
}
$startRow_fase = $pageNum_fase * $maxRows_fase;
mysql_select_db($database_dbssantos, $dbssantos);
$query_fase = "SELECT NOMFASES FROM tblfases";
$query_limit_fase = sprintf("%s LIMIT %d, %d", $query_fase, $startRow_fase, $maxRows_fase);
$fase = mysql_query($query_limit_fase, $dbssantos) or die(mysql_error());
$row_fase = mysql_fetch_assoc($fase);
if (isset($_GET['totalRows_fase'])) {
$totalRows_fase = $_GET['totalRows_fase'];
} else {
$all_fase = mysql_query($query_fase);
$totalRows_fase = mysql_num_rows($all_fase);
}
$totalPages_fase = ceil($totalRows_fase/$maxRows_fase)-1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Documento sin título</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="POST" name="insertar" id="insertar">
<div align="center">
<p>
<select name="proyecto" id="proyecto">
<?php
do {
?>
<option value="<?php echo $row_proyecto['CODPRO']?>"><?php echo $row_proyecto['NOMPRO']?></option>
<?php
} while ($row_proyecto = mysql_fetch_assoc($proyecto));
$rows = mysql_num_rows($proyecto);
if($rows > 0) {
mysql_data_seek($proyecto, 0);
$row_proyecto = mysql_fetch_assoc($proyecto);
}
?>
</select>
</p>
<table border="1">
<tr>
<td>NOMFASES</td>
<td>CODIGO</td>
<td>DURACION</td>
<td>OBSERVACION</td>
<td>AGREGAR</td>
</tr>
<?php do { ?>
<tr>
<td><input name="NOMBRE[]" type="text" id="NOMBRE" value="<?php echo $row_fase['NOMFASES']; ?>" size="40"> </td>
<td><input name="CODIGO[]" type="text" id="CODIGO" size="10">
</td>
<td><input name="DURACION[]" type="text" id="DURACION" size="10">
</td>
<td><textarea name="OBSERVACION" id="OBSERVACION"></textarea>
</td>
<td><input name="inser[]" type="checkbox" id="inser[]" value="=<?=$row['CODSBF'];?>">
</td>
</tr>
<?php } while ($row_fase = mysql_fetch_assoc($fase)); ?>
</table>
<p>
<input type="submit" name="Submit" value="Insertar">
</p>
</div>
<input type="hidden" name="MM_insert" value="insertar">
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($proyecto);
mysql_free_result($fase);
?>
La parte de código a editar es esta:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertar")) {
$insertSQL = sprintf("INSERT INTO tblfas (CODFAS, NOMFAS, DURFAS, OBSFAS, CODPRO) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['CODIGO'], "text"),
GetSQLValueString($_POST['NOMBRE'], "text"),
GetSQLValueString($_POST['DURACION'], "double"),
GetSQLValueString($_POST['OBSERVACION'], "text"),
GetSQLValueString($_POST['proyecto'], "text"));
mysql_select_db($database_dbssantos, $dbssantos);
$Result1 = mysql_query($insertSQL, $dbssantos) or die(mysql_error());
}
Sustitúyelo por esto:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertar")) {
$codigo = $_POST['CODIGO'];
$nombre = $_POST['NOMBRE'];
$duracion = $_POST['DURACION'];
$observacion = $_POST['OBSERVACION'];
$proyecto = $_POST['proyecto'];
$inser = $_POST['inser'];
//recorremos el array para comprobar los campos marcados
for($i=0;$i<count($inser);$i++) {
if($inser[$i]) { //si está marcado ejecutamos el insert
$insertSQL = sprintf("INSERT INTO tblfas (CODFAS, NOMFAS, DURFAS, OBSFAS, CODPRO) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($codigo[$i], "text"),
GetSQLValueString($nombre[$i], "text"),
GetSQLValueString($duracion[$i], "double"),
GetSQLValueString($observacion[$i], "text"),
GetSQLValueString($proyecto[$i], "text"));
mysql_select_db($database_dbssantos, $dbssantos);
$Result1 = mysql_query($insertSQL, $dbssantos) or die(mysql_error());
}
}
}
Añádele al textarea OBSERVACION los corchetes [] sinó se machacarán los valores.
Creo q con esto basta para que funcione correctamente. Ya me contarás.
Salu2,
iNphYx

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas