Foros del Web » Programando para Internet » PHP »

array en registro mysql

Estas en el tema de array en registro mysql en el foro de PHP en Foros del Web. Que tal amigos, tengo una pequeña duda... estoy haciendo un pequeño sistema de registro, y uno de los campos (En este caso en el campo ...
  #1 (permalink)  
Antiguo 26/09/2007, 11:32
 
Fecha de Ingreso: junio-2005
Ubicación: Edo. Mex
Mensajes: 725
Antigüedad: 18 años, 10 meses
Puntos: 5
Sonrisa array en registro mysql

Que tal amigos, tengo una pequeña duda... estoy haciendo un pequeño sistema de registro, y uno de los campos (En este caso en el campo Deporte), el usuario tiene que poder seleccionar mas de una opción en Deporte, es decir tengo que almacenar ese dato como un grupo, es con un array, pero ya me atore con esto.. llevo un ratito con esto y no me sale... hace un tiempo que no lo hacia ...

Les muestro:
Código PHP:
<?php require_once('../Connections/conexion.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 usuarios (id_user, Nombre, Apellidos, Mail, Edad, Empresa, Dir, Ciudad, Telefono, Deporte, Password) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['id_user'], "int"),
                       
GetSQLValueString($_POST['Nombre'], "text"),
                       
GetSQLValueString($_POST['Apellidos'], "text"),
                       
GetSQLValueString($_POST['Mail'], "text"),
                       
GetSQLValueString($_POST['Edad'], "text"),
                       
GetSQLValueString($_POST['Empresa'], "text"),
                       
GetSQLValueString($_POST['Dir'], "text"),
                       
GetSQLValueString($_POST['Ciudad'], "text"),
                       
GetSQLValueString($_POST['Telefono'], "text"),
                       
GetSQLValueString($_POST['Deporte'], "text"),
                       
GetSQLValueString($_POST['Password'], "text"));

  
mysql_select_db($database_conexion$conexion);
  
$Result1 mysql_query($insertSQL$conexion) or die(mysql_error());

  
$insertGoTo "conf_registro.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
    
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$insertGoTo));
}

session_start();
$_SESSION['nom']=$_POST['Nombre']; 
$_SESSION['mai']=$_POST['Mail']; 
$_SESSION['pass']=$_POST['Password']; 
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Registro de Usuarios</title>
</head>

<body>
<!-- Formulario del registro -->
Los campos marcados con (*) son obligatorios
<form method="post" name="form1" action="<?php echo $editFormAction?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">(*) Nombre:</td>
      <td><input type="text" name="Nombre" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">(*) Apellidos:</td>
      <td><input type="text" name="Apellidos" value="" size="50"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">(*) Mail:</td>
      <td><input type="text" name="Mail" value="" size="50"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Edad:</td>
      <td><input type="text" name="Edad" value="" size="10"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Empresa:</td>
      <td><input type="text" name="Empresa" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Direcci&oacute;n:</td>
      <td><input type="text" name="Dir" value="" size="50"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Ciudad:</td>
      <td><input type="text" name="Ciudad" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Tel&eacute;fono:</td>
      <td><input type="text" name="Telefono" value="" size="16"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Deporte:</td>
      <td>
      <input type="checkbox" name="Deporte[]" value="Rugby" > 
      Rugby<br />
      <input name="Deporte[]" type="checkbox" value="Vela" />
      Vela<br />
      <label>
      <input name="Deporte[]" type="checkbox" value="Ecuestre" />
      Ecuestre<br />
      <input name="Deporte[]" type="checkbox" value="Tenis" />
      Tenis<br />
      <input name="Deporte[]" type="checkbox" value="Golf" />
      Golf
      </label></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">(*) Password:</td>
      <td><input type="password" name="Password" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Registrarme"></td>
    </tr>
  </table>
  <input type="hidden" name="id_user" value="">
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<!--Termina formulario del registro -->
</body>
</html>
Gracias de antemano, solo lo que tengo que hacer es mandar correctamente el array, pero estoy atorado, ojala me puedan ayudar..

Les agradezco de antemano, un saludo cordial..

Ricardo.
  #2 (permalink)  
Antiguo 26/09/2007, 17:01
Avatar de kunndry  
Fecha de Ingreso: abril-2004
Ubicación: Alicante / España
Mensajes: 247
Antigüedad: 20 años
Puntos: 0
Re: array en registro mysql

En mysql si no me confundo no se pueden guardar arrays. La solución más ortodoxa seria que cada uno de los elementos del array de los deportes los guardaras en otra tabla la cual deberia ser:
DeportesUsuario
Usuario
Deporte
Y que por otro lado, tuvieses otra tabla, con los deportes, pero si no vas hacer la base de datos con claves primarias y ajenas (no te lo recomiendo) podrias saltartelo.
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 12:09.