Foros del Web » Programando para Internet » PHP »

problemas al insertar a una base de datos

Estas en el tema de problemas al insertar a una base de datos en el foro de PHP en Foros del Web. holas a todos tengo un problema al insertar a una base de datos MySql usando php al aceptar el insert me sale este error en ...
  #1 (permalink)  
Antiguo 08/05/2011, 01:24
 
Fecha de Ingreso: febrero-2011
Mensajes: 18
Antigüedad: 13 años, 2 meses
Puntos: 0
Pregunta problemas al insertar a una base de datos

holas a todos tengo un problema al insertar a una base de datos MySql usando php

al aceptar el insert me sale este error en la pantalla
Cita:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '±a) VALUES ('1', '1', '1', 1, '1', '1', '1', '1')' at line 1
este es mi código:

Cita:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_clasic = "localhost";
$database_clasic = "classictravel";
$username_clasic = "root";
$password_clasic = "vertrigo";
$clasic = mysql_pconnect($hostname_clasic, $username_clasic, $password_clasic) or trigger_error(mysql_error(),E_USER_ERROR);
?>
Cita:
<?php require_once('Connections/clasic.php'); ?>
<?php
if (!function_exists('GetSQLValueString')) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$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 personal (nombre, apaterno, amaterno, ci, depci, rango, codigo, contraseña) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['nombre'], "text"),
GetSQLValueString($_POST['apaterno'], "text"),
GetSQLValueString($_POST['amaterno'], "text"),
GetSQLValueString($_POST['ci'], "int"),
GetSQLValueString($_POST['depci'], "text"),
GetSQLValueString($_POST['rango'], "text"),
GetSQLValueString($_POST['codigo'], "text"),
GetSQLValueString($_POST['contrasea'], "text"));

mysql_select_db($database_clasic, $clasic);
$Result1 = mysql_query($insertSQL, $clasic) or die(mysql_error());

$insertGoTo = "prueba.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!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=utf-8" />
<title>Documento sin título</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Nombre:</td>
<td><input type="text" name="nombre" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Apaterno:</td>
<td><input type="text" name="apaterno" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Amaterno:</td>
<td><input type="text" name="amaterno" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Ci:</td>
<td><input type="text" name="ci" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Depci:</td>
<td><input type="text" name="depci" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Rango:</td>
<td><input type="text" name="rango" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Codigo:</td>
<td><input type="text" name="codigo" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Contraseña:</td>
<td><input type="text" name="contrasea" value="" size="32" /></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>
</body>
</html>
estoy usando el vertrigoserver y es la primera ves que me aparece este tipo de mensajes les agradecería mucho si pueden ayudarme con este problema

gracias
  #2 (permalink)  
Antiguo 12/08/2011, 15:16
Avatar de tomark4  
Fecha de Ingreso: septiembre-2007
Mensajes: 154
Antigüedad: 16 años, 7 meses
Puntos: 29
Respuesta: problemas al insertar a una base de datos

estas usando codigo del asistente de dreamweaver que por lo general
crea muchas lineas dificiles de depurar que a la hora de un error
es dificil de encontrar te sugiero que hagas tu codigo a mano
para insertar un registro en tu base de datos.

te coloco un ejemplo que luego tu podras modificar segun tus campos
y tu base de datos
lo primero seria crear un html que tendra tu formulario

a este yo lo llamare formulario.html y quedarias asi
en este ejemplo pondre 4 campos cedula, nombre apellido y direccion

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Ingreso de Nuevo Registro</title>
  5. </head>
  6.  
  7. <?php echo $_REQUEST[msj]."<br>"?>
  8. <form action="NuevoRegistro.php" method="post">
  9. <table width="200" border="0">
  10.   <caption>
  11.     Ingreso de Nuevo registro
  12.   </caption>
  13.   <tr>
  14.     <td>cedula</td>
  15.     <td><input type="text" name="cedula" id="cedula" /></td>
  16.   </tr>
  17.   <tr>
  18.     <td>nombre</td>
  19.     <td><input type="text" name="nombre" id="nombre" /></td>
  20.   </tr>
  21.   <tr>
  22.     <td>direccion</td>
  23.     <td><textarea name="direccion" id="direccion" cols="45" rows="5"></textarea></td>
  24.   </tr>
  25.   <tr>
  26.     <td>telefono</td>
  27.     <td><input type="text" name="telefono" id="telefono" /></td>
  28.   </tr>
  29.   <tr>
  30.     <td colspan="2" align="center"><input type="submit" name="guardar" id="guardar" value="Guardar" /></td>
  31.   </tr>
  32. </form>
  33. </body>
  34. </html>

luego tendriamos el script de php que recoge los datos al cual llamare
NuevoRegistro.php

Código PHP:
Ver original
  1. //estableces los parametros de conexion con tu bdd
  2. $user = "tuusuario";
  3. $passwd = "tupass";
  4. $server = "localhost";
  5. $bdd = "basededatos";
  6.  
  7. //creo la cadena de conexion
  8. $cn = mysql_connect($server, $user, $pass) or die(mysql_error());
  9.  
  10. //selecciono la base de datos a usar
  11.  
  12. // recojo las variables enviadas por el formulario
  13. $nombre = $_POST[nombre];
  14. $cedula = $_POST[cedula];
  15. $direccion = $_POST[direccion];
  16. $telefono =  $_POST[telefono];
  17.  
  18. //creo el query de insercion en MYSQL
  19. $sql = "INSERT INTO agenda (cedula, nombre, direccion, telefono)
  20. VALUES('$nombre', '$cedula', '$direccion', '$telefono')";
  21. //creo el recordset con el query para ejecutarlo
  22. $rs = mysql_query($sql) or die(mysql_error());
  23.  
  24. //verifico si los datos se agregaron o no
  25.     //si agrego el registro sin error muestro un simple mensaje
  26.     echo "Tus datos se han agregado exitosamente";
  27. }
  28. else{
  29.     //caso contrario que haya habido error lo reenvio a la pagina del formulario
  30.     $msj = "se ha producido un error intente de nuevo";
  31.     header('Location:formulario.php?mensaje=$msj');
  32. }

con este sencillo ejemplo lograrias realizar tu insercion
a la vez que la depuracion de errores seria mucho mas facil
falta que arregles con tu formulario mejores y agregues segun
tus requerimientos espero haberte ayudado.

Etiquetas: Ninguno
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:28.