Ver Mensaje Individual
  #3 (permalink)  
Antiguo 29/04/2010, 07:30
diegoba
 
Fecha de Ingreso: abril-2010
Mensajes: 4
Antigüedad: 14 años, 10 meses
Puntos: 0
Respuesta: Como inserto varios registros desde un multiselect

Hola juaniquillo, muchas gracias por responder. Agrege el codigo como sugeriste pero agrega solo un registro de los que selecciono. El codigo es:
Cita:
<?php require_once('Connections/prueba.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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"] == "form1")) {
foreach($_POST["Analisis"] as $key => $value){
$insertSQL = sprintf("INSERT INTO b (id_a, Analisis) VALUES (%s, %s)",
GetSQLValueString($_POST['id_a'][$key], "int"),
GetSQLValueString($_POST['Analisis'][$key], "text"));

mysql_select_db($database_prueba, $prueba);
$Result1 = mysql_query($insertSQL, $prueba) or die(mysql_error());

$insertGoTo = "datos.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
}
mysql_select_db($database_prueba, $prueba);
$query_id = "SELECT id FROM a ORDER BY a.id DESC LIMIT 1";
$id = mysql_query($query_id, $prueba) or die(mysql_error());
$row_id = mysql_fetch_assoc($id);
$totalRows_id = mysql_num_rows($id);

mysql_select_db($database_prueba, $prueba);
$query_tec = "SELECT tec.Tec FROM tec";
$tec = mysql_query($query_tec, $prueba) or die(mysql_error());
$row_tec = mysql_fetch_assoc($tec);
$totalRows_tec = mysql_num_rows($tec);
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Id_a:</td>
<td><input type="text" name="id_a" value="<?php echo $row_id['id']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap>Analisis:</td>
<td><label>
<select name="Analisis[]" size="10" multiple id="Analisis">
<?php
do {
?>
<option value="<?php echo $row_tec['Tec']?>"><?php echo $row_tec['Tec']?></option>
<?php
} while ($row_tec = mysql_fetch_assoc($tec));
$rows = mysql_num_rows($tec);
if($rows > 0) {
mysql_data_seek($tec, 0);
$row_tec = mysql_fetch_assoc($tec);
}
?>
</select>
</label></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Insertar registro"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html><?php
mysql_free_result($id);

mysql_free_result($tec);
?>
Si podes verlo seria bueno, para encontrarle la vuelta a este problema.
Desde ya muchas gracias.
Diego