PHP - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

 
Vista:
sin imagen de perfil

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Publicado por Martin (4 intervenciones) el 12/01/2018 04:13:46
Hola que tal soy nuevo en php quiero hacer un simple alta de nuevo registros en una BD de mysql pero me sale un erro e intentado todo pero estoy desesperado y no se que hacer, solo quiero dar de alta 2 simples registros de texto a una tabla de myql pero me da un error

se envio el formulario
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 ''', '')' at line 1


Este es mi codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!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>Practica PHP</title>
</head>
<body>
 
<?php
include("conexion.php");
 
 
  if(isset($_POST['registrar']))
   {
        echo 'se envio el formulario';
        echo "<br>";
 
        $usuario = $_POST['user'];
		  $pass = $_POST['pass'];
 
 
		$q = "INSERT INTO simple(Nombre, Contrasena) VALUES '$usuario', '$pass')";
 
		//Run Query
		$result = mysql_query($q) or die(mysql_error());
    }
     mysql_close();
 
 
 
 
?>
<form name="formulario" method="post" action="">
 
    <input type="text" name="user" maxlength="30" size="40">
 
    <input type="password" name="pass" maxlength="30" size="40">
 
    <input  type="submit"  value="Registrar" name="registrar">
 
</form>
 
 
<br />
<a href="index.php">Regresar</a>
 
 
 
</body>
</html>
 
<--Este es mo archivo de conexion.-->
<?php
// datos para la coneccion a mysql 
$hostdb= "localhost";
$userdb= "root";
$passdb= "undertaker";
$db= "general2018";
 
//Realizamos la conexion
$conexion = mysql_connect($hostdb,$userdb,$passdb);
 
//Estructura de control por si falla
if(!$conexion)
{
die("No he podido conectar porque: ".mysql_error());
}
else {
	//echo "si se conecto a bd";
}
//Seleccion la base de datos
mysql_select_db($db,$conexion);
 
?>

Este es el index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!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>Practica PHP</title>
</head>
<body>
 
<a href="Practica.php">Insertar nuevo usuario</a>
<br />
<a href="consultar.php">Consultar usuarios</a>
<br />
<a href="modificar.php">Modificar usuario</a>
<br />
<a href="eliminar.php">Eliminar usuario</a>
<br />
 
</body>
</html>



ya verifique y si se contecta a una BD y todo esta bien, pero no puedeo dar de alta un simple registro...

Anexo el archivo index y el archivo alta ojala me puedan ayudar...
Muchas gracias Dios les bendiga.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 604
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Publicado por Miguel (218 intervenciones) el 12/01/2018 05:48:58
1
$q = "INSERT INTO simple(Nombre, Contrasena) VALUES '$usuario', '$pass')";

Pues en esta fila, al final tienes un paréntesis ) extra, quítalo. Por otra parte, en las nuevas versiones de PHP tendrás que usar mysqli, también te recomiendo usar consultas parametrizadas para evitar inyecciones SQL.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Publicado por xve (6935 intervenciones) el 12/01/2018 08:13:30
Hola Miguel, yo creo que le falta un paréntesis, no? algo así:
1
$q = "INSERT INTO simple(Nombre, Contrasena) VALUES ('$usuario', '$pass')";
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 604
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Publicado por Miguel (218 intervenciones) el 12/01/2018 12:46:13
Gracias por la corrección @xve, en efecto, le falta un paréntesis. Creo que ando un poco dormido jejeje.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Georkis
Val: 75
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Publicado por Georkis (32 intervenciones) el 13/01/2018 16:35:40
Hola te recomiendo para la password usar una encrytación adecuada, no hablo de la función md5... aplicar un hash interactivo... para evitar que la password colapcen y sea vulerable... y tambien no debes de guardar la password tal como es en la base de datos...

Se que no es el tema de este problema, pero es un problema de seguridad que estoy viendo en tu código... Bueno estamos para ayudar, si tienes una duda al respecto, abres un tema referente a esto y yo te ayudo....
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Publicado por Muchas gracias! si me funciono! (4 intervenciones) el 12/01/2018 17:58:28
Muchas gracias! si me funciono!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar