Comunidad de diseño web y desarrollo en internet online

Insertar registros 2 tablas

Citar            
MensajeEscrito el 26 May 2008 09:15 am
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 :

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 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í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ó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º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º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á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º 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º 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ó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í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\"> </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> </p>
</body>
</html>

Por Hamwe

7 de clabLevel



 

firefox
Citar            
MensajeEscrito el 27 May 2008 07:47 am
Tras modificar el código el formulario hace lo siguiente, cuando envío en la tabla personas se me crea un nuevo registro con todosl os campos null y en la tabla tags el explode no hace nada y se me crea otro registro con todos los campos tambien null

Código :

<?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>

Por Hamwe

7 de clabLevel



 

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.