Foros del Web » Programando para Internet » PHP »

Insertar registros 2 tablas

Estas en el tema de Insertar registros 2 tablas en el foro de PHP en Foros del Web. Tengo una duda El otro dia configuré la forma en que con un explode cortara la cadena y me separara las palabras en una tabla ...
  #1 (permalink)  
Antiguo 26/05/2008, 02:16
 
Fecha de Ingreso: abril-2008
Mensajes: 70
Antigüedad: 16 años
Puntos: 0
Insertar registros 2 tablas

Tengo una duda

El otro dia configuré la forma en que con un explode cortara la cadena y me separara las palabras en una tabla el codigo es el siguiente.

Código PHP:
if ($tags = isset($_POST["tags"]) ? $_POST["tags"] : false); {
$tags explode(","$tags);
$insertSQL "insert into pais (tags) values \n";
$contador 0;
foreach(
$tags as $item){
if(
$contador$insertSQL .= ", ";
$contador++;
$insertSQL .= "\n('$item')"
Con esto yo introduzco en el formulario Casa, coche, arbol

Y en mi base de datos en la tabla tags aparece por separado cada una de las palabras que añadi con su id respectiva.

Ahora quiero que en el formulario que tengo yo agrego a los primeros campos la información sobre el contacto que quiero añadir a mi base de datos (nombre, apellidos,tlf etc...) y esos datos se insertan en la tabla personas y luego tengo otro campo del fomulario que se denomina tags en el que quiero que se pueda clasificiar ese contacto que acabo de añadir (escritor, valenciano, español) por ejemplo. Esos tags que le meta el explode me separa las comas y me los mete en otra tabla llamada tags. A la hora de unificar los 2 formularios no soy capaz de que el explode me rule y me separe la cadena.

Si alguien me puede echar una mano se lo agradezco

Código PHP:
<?php require_once('Connections/bd_contactos.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"] == "form1")) {
$insertSQL sprintf("INSERT INTO persoas (cod_prsa, cod_nme, cod_aplds, txt_nome_aclme, txt_aplds_aclme, txt_tlfno_trbllo, txt_tlfno_dmclo, txt_tlfno_mbl, txt_fx, cod_crro_1, cod_crro_2, cod_url, cod_ra, cod_nmr_ra, cod_nmr_pso, cod_endrzo_info_pls, cod_cp, cod_pblcn, cod_prvnca, cod_pis) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['cod_prsa'], "int"),
GetSQLValueString($_POST['cod_nme'], "text"),
GetSQLValueString($_POST['cod_aplds'], "text"),
GetSQLValueString($_POST['txt_nome_aclme'], "text"),
GetSQLValueString($_POST['txt_aplds_aclme'], "text"),
GetSQLValueString($_POST['txt_tlfno_trbllo'], "text"),
GetSQLValueString($_POST['txt_tlfno_dmclo'], "text"),
GetSQLValueString($_POST['txt_tlfno_mbl'], "text"),
GetSQLValueString($_POST['txt_fx'], "text"),
GetSQLValueString($_POST['cod_crro_1'], "text"),
GetSQLValueString($_POST['cod_crro_2'], "text"),
GetSQLValueString($_POST['cod_url'], "text"),
GetSQLValueString($_POST['cod_ra'], "text"),
GetSQLValueString($_POST['cod_nmr_ra'], "text"),
GetSQLValueString($_POST['cod_nmr_pso'], "text"),
GetSQLValueString($_POST['cod_endrzo_info_pls'], "text"),
GetSQLValueString($_POST['cod_cp'], "text"),
GetSQLValueString($_POST['cod_pblcn'], "text"),
GetSQLValueString($_POST['cod_prvnca'], "text"),
GetSQLValueString($_POST['cod_pis'], "text"));
$insertSQL sprintf("INSERT INTO tags (cod_tag, txt_tag, tpo_tag) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['cod_tag'], "int"),
GetSQLValueString($_POST['txt_tag'], "text"),
GetSQLValueString($_POST['tpo_tag'], "text"));

mysql_select_db($database_bd_contactos$bd_contactos);
$Result1 mysql_query($insertSQL$bd_contactos) or die(mysql_error());

 
mysql_select_db($database_bd_contactos$bd_contactos);
$Result12 mysql_query($insertSQL2$bd_contactos) or die(mysql_error()); 
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<style type="text/css">
<!--
.Estilo1 {
font-family: Georgia, "Times New Roman", Times, serif;
font-weight: bold;
}
-->
</style>
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction?>">
<table align="center">
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Nome:</span></td>
<td><input type="text" name="cod_nme" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Apelidos:</span></td>
<td><input type="text" name="cod_aplds" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Alcume nome:</span></td>
<td><input type="text" name="txt_nome_aclme" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Alcume apelidos:</span></td>
<td><input type="text" name="txt_aplds_aclme" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Tlf traballo:</span></td>
<td><input type="text" name="txt_tlfno_trbllo" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Tlf domicilio:</span></td>
<td><input type="text" name="txt_tlfno_dmclo" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Tlf m&oacute;bil:</span></td>
<td><input type="text" name="txt_tlfno_mbl" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Fax:</span></td>
<td><input type="text" name="txt_fx" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">email n&ordm;1:</span></td>
<td><input type="text" name="cod_crro_1" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Email n&ordm;2:</span></td>
<td><input type="text" name="cod_crro_2" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">P&aacute;xina web:</span></td>
<td><input type="text" name="cod_url" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Domicilio:</span></td>
<td><input type="text" name="cod_ra" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">N&ordm; domicilio:</span></td>
<td><input type="text" name="cod_nmr_ra" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">N&ordm; Piso:</span></td>
<td><input type="text" name="cod_nmr_pso" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Cod_endrzo_info_pls:</span></td>
<td><input type="text" name="cod_endrzo_info_pls" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">CP:</span></td>
<td><input type="text" name="cod_cp" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Poboaci&oacute;n:</span></td>
<td><input type="text" name="cod_pblcn" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Provincia:</span></td>
<td><input type="text" name="cod_prvnca" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap"><span class="Estilo1">Pa&iacute;s:</span></td>
<td><input type="text" name="cod_pis" value="" size="32"></td>
</tr>
<td nowrap align="right"><span class="Estilo1">Tag:</span></td>
<td><input type="text" name="txt_tag" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><span class="Estilo1">Tipo Tag:</span></td>
<td><input type="text" name="tpo_tag" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><div align="center">
<input type="submit" value="Insertar registro">
</div></td>
</tr>
</table>
<input type="hidden" name="cod_prsa" value="">
<input type="hidden" name="cod_tag" value="">
<input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
  #2 (permalink)  
Antiguo 26/05/2008, 09:32
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Insertar registros 2 tablas

Hola Hamwe,

El problema es que tu estás duplicando tu variable $insertSQL, por ende no te sirve ya que duplicas tu query (que en este caso es la ultima).

Te recomiendo que le cambies el nombre a tu segundo Query, y lances los procesos por separado, es decir:
- Construyes primer query
- Lo envias a la BDD
- Construyes segundo query
- Lo envias al a BDD.

Saludos.
  #3 (permalink)  
Antiguo 27/05/2008, 01:40
 
Fecha de Ingreso: abril-2008
Mensajes: 70
Antigüedad: 16 años
Puntos: 0
Respuesta: Insertar registros 2 tablas

Así hice le cambié el nombre al 2º query pero el explode no hace nada, cuando envío el formulario se me agrega a cada tabla un nuevo registro que en cada campo pone null

Código PHP:
<?php require_once('Connections/bd_contactos.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 (
$tags = isset($_POST["MM_insert"]) && ($_POST["MM_insert"] == "form1"));
  
$tags explode(","$tags);

 
$insertSQL sprintf("INSERT INTO persoas (cod_prsa, cod_nme, cod_aplds, txt_nome_aclme, txt_aplds_aclme, txt_tlfno_trbllo, txt_tlfno_dmclo, txt_tlfno_mbl, txt_fx, cod_crro_1, cod_crro_2, cod_url, cod_ra, cod_nmr_ra, cod_nmr_pso, cod_endrzo_info_pls, cod_cp, cod_pblcn, cod_prvnca, cod_pis) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['cod_prsa'], "int"),
                       
GetSQLValueString($_POST['cod_nme'], "text"),
                       
GetSQLValueString($_POST['cod_aplds'], "text"),
                       
GetSQLValueString($_POST['txt_nome_aclme'], "text"),
                       
GetSQLValueString($_POST['txt_aplds_aclme'], "text"),
                       
GetSQLValueString($_POST['txt_tlfno_trbllo'], "text"),
                       
GetSQLValueString($_POST['txt_tlfno_dmclo'], "text"),
                       
GetSQLValueString($_POST['txt_tlfno_mbl'], "text"),
                       
GetSQLValueString($_POST['txt_fx'], "text"),
                       
GetSQLValueString($_POST['cod_crro_1'], "text"),
                       
GetSQLValueString($_POST['cod_crro_2'], "text"),
                       
GetSQLValueString($_POST['cod_url'], "text"),
                       
GetSQLValueString($_POST['cod_ra'], "text"),
                       
GetSQLValueString($_POST['cod_nmr_ra'], "text"),
                       
GetSQLValueString($_POST['cod_nmr_pso'], "text"),
                       
GetSQLValueString($_POST['cod_endrzo_info_pls'], "text"),
                       
GetSQLValueString($_POST['cod_cp'], "text"),
                       
GetSQLValueString($_POST['cod_pblcn'], "text"),
                       
GetSQLValueString($_POST['cod_prvnca'], "text"),
                       
GetSQLValueString($_POST['cod_pis'], "text"));
    
$insertSQL2 "insert into tags (text_tag) values \n";
                       
GetSQLValueString($_POST['text_tag'], "text"),
                       
GetSQLValueString($_POST['tpo_tag'], "text"));
$contador 0;
foreach(
$tags as $item){
    if(
$contador)  $insertSQL2 .= ", ";
    
$contador++;
    
$insertSQL2 .= "\n('$item')";


  
mysql_select_db($database_bd_contactos$bd_contactos);
  
$Result1 mysql_query($insertSQL$bd_contactos) or die(mysql_error());
  
   
mysql_select_db($database_bd_contactos$bd_contactos);
  
$Result12 mysql_query($insertSQL2$bd_contactos) or die(mysql_error());
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<style type="text/css">
<!--
.Estilo1 {
    font-family: Georgia, "Times New Roman", Times, serif;
    font-weight: bold;
}
-->
</style>
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction?>">
  <table align="center">
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Nome:</span></td>
      <td><input type="text" name="cod_nme" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Apelidos:</span></td>
      <td><input type="text" name="cod_aplds" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Alcume nome:</span></td>
      <td><input type="text" name="txt_nome_aclme" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Alcume apelidos:</span></td>
      <td><input type="text" name="txt_aplds_aclme" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Tlf traballo:</span></td>
      <td><input type="text" name="txt_tlfno_trbllo" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Tlf domicilio:</span></td>
      <td><input type="text" name="txt_tlfno_dmclo" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Tlf m&oacute;bil:</span></td>
      <td><input type="text" name="txt_tlfno_mbl" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Fax:</span></td>
      <td><input type="text" name="txt_fx" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">email n&ordm;1:</span></td>
      <td><input type="text" name="cod_crro_1" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Email n&ordm;2:</span></td>
      <td><input type="text" name="cod_crro_2" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">P&aacute;xina web:</span></td>
      <td><input type="text" name="cod_url" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Domicilio:</span></td>
      <td><input type="text" name="cod_ra" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">N&ordm; domicilio:</span></td>
      <td><input type="text" name="cod_nmr_ra" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">N&ordm; Piso:</span></td>
      <td><input type="text" name="cod_nmr_pso" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Cod_endrzo_info_pls:</span></td>
      <td><input type="text" name="cod_endrzo_info_pls" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">CP:</span></td>
      <td><input type="text" name="cod_cp" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Poboaci&oacute;n:</span></td>
      <td><input type="text" name="cod_pblcn" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Provincia:</span></td>
      <td><input type="text" name="cod_prvnca" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap"><span class="Estilo1">Pa&iacute;s:</span></td>
      <td><input type="text" name="cod_pis" value="" size="32"></td>
    </tr>
  <td nowrap align="right"><span class="Estilo1">Tag:</span></td>
      <td><input type="text" name="txt_tag" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><span class="Estilo1">Tipo Tag:</span></td>
      <td><input type="text" name="tpo_tag" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><div align="center">
        <input type="submit" value="Insertar registro">
      </div></td>
    </tr>
  </table>
  <input type="hidden" name="cod_prsa" value="">
    <input type="hidden" name="cod_tag" value="">
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:19.