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

Hola Gente veo que hay muchos que la tiene muy clara.
Estoy intentando insertar en una tabla varios registros tomados de un multiselect de una Lista/Menu.
El codigo que generé es el siguiente:
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")) {
$insertSQL = sprintf("INSERT INTO b (Analisis) VALUES (%s)",
GetSQLValueString($_POST['det'], "text"));

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

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO b (id_a) VALUES (%s)",
GetSQLValueString($_POST['textfield'], "int"));

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

mysql_select_db($database_prueba, $prueba);
$query_tec = "SELECT 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);

mysql_select_db($database_prueba, $prueba);
$query_id = "SELECT a.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);
?>
<p>&nbsp;</p>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<p>
<label>
<input name="textfield" type="text" id="textfield" value="<?php echo $row_id['id']; ?>" size="6">
<br>
</label>
<select name="det" size="10" multiple>
<?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>
</p>
<p>
<label>
<input type="submit" name="envio" id="envio" value="Enviar">
</label>
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
<?php
mysql_free_result($tec);

mysql_free_result($id);
?>
Ahora esto me tira un error el siguiente:

"Cannot add or update a child row: a foreign key constraint fails (`p/b`, CONSTRAINT `b_ibfk_1` FOREIGN KEY (`id_a`) REFERENCES `a` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)"

Le encontré una vuelta no muy profesional que es la siguiente, pero me gustaría la primera:

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;
}
}

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']);
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO b (id_a, Analisis) VALUES (%s, %s)",
GetSQLValueString($_POST['id_a'], "int"),
GetSQLValueString($_POST['Analisis'], "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));
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO b (id_a, Analisis) VALUES (%s, %s)",
GetSQLValueString($_POST['id_a'], "int"),
GetSQLValueString($_POST['select'], "text"));

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

mysql_select_db($database_prueba, $prueba);
$query_tec = "SELECT * FROM tec ORDER BY Tec ASC";
$tec = mysql_query($query_tec, $prueba) or die(mysql_error());
$row_tec = mysql_fetch_assoc($tec);
$totalRows_tec = mysql_num_rows($tec);

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);
?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Id_a:</td>
<td><input type="text" name="id_a" value="<?php echo $row_id['id']; ?>" size="32" readonly="readonly"/></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Analisis:</td>
<td><label>
<select name="Analisis" 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="nowrap" align="right">Analisis</td>
<td><label>
<select name="select" id="select">
<?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="nowrap" align="right">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign="baseline">
<td nowrap="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>

<?php
mysql_free_result($tec);

mysql_free_result($id);
?>
Esta si anda.
Como podría hacer lo mismo pero con la primera? A alguien se le ocurre algo?
Desde ya muchas gracias, excelente foro!!
Diego