FranP FranP - 8 days ago 5x
MySQL Question

Mysqli_query() INSERT INTO doesn´t work

could someone help me? I don´t know why this "INSERT INTO" query doesn´t work. With the echo it seems everything is OK.

The echo in the code seems to return the right query, but I don´t know why the query doesn´t execute...




$conexion=mysqli_connect('localhost', 'root','','redsocial') or die("oh no, problemas tecnicos");

$resultadoConsulta=mysqli_query($conexion,"select * from usuarios where nick= '".$_POST['nick']."' or correo= '".$_POST['correo']."'");



$consulta2="insert into usuarios (nick,password,nombre,apellidos,direccion,telefono,correo,foto) VALUES ('".$_POST['nick']."','".$passCifrado."','".$_POST['nombre']."','".$_POST['apellidos']."','".$_POST['direccion']."',".$_POST['telefono'].",'".$_POST['correo']."',NULL);";

echo $consulta2;

echo "registro insertado!";

echo "El registro ya existe";





The return code from the echo would be:

insert into usuarios (nick,password,nombre,apellidos,direccion,telefono,correo,foto) VALUES ('myNick_String','81dc9bdb52d04dc20036dbd8313ed055','myName_String','mySurname_String','myAddress_String',94438,'xxx@xmail.com_Str',NULL);

All the fields are VARCHAR except "telefono" which is INT. The "foto" can be NULL.

Of course it is only a test exercise, thats why root has no pass and so :)


I have solved it! As i can´t add any answer, I edit to inform that thanks to this answer, given in a link to me by @Drew, I could find that the problem was, my password field was varchar(30), while the md5 encryption was setting a varchar with more length than 30... so I solved it with an alter table and changing the data type to a more lengthy varchar.

Thanks to all fo you, I have learned many important, basic things; so, again thank you all. People like you make this site great.


Your problem is incorrect formatting, BE SURE to remember all the quotes...

Your value:

$consulta2="insert into usuarios (nick,password,nombre,apellidos,direccion,telefono,correo,foto) VALUES ('".$_POST['nick']."','".$passCifrado."','".$_POST['nombre']."','".$_POST['apellidos']."','".$_POST['direccion']."',".$_POST['telefono'].",'".$_POST['correo']."',NULL);";
/*                                                                                                                                                                                                                          ^                    ^*/

Fixed value:

$consulta2="insert into usuarios (nick,password,nombre,apellidos,direccion,telefono,correo,foto) VALUES ('".$_POST['nick']."','".$passCifrado."','".$_POST['nombre']."','".$_POST['apellidos']."','".$_POST['direccion']."','".$_POST['telefono']."','".$_POST['correo']."',NULL);";

EDIT: Why do I need those quotes for an integer?

Ever value will need quotes, even if it is a integer, if you are uploading NULL it does not require quotes. The database will still handle it as an integer, but the quotes are needed to understand what value to send. When the database receives the value it will store is as an "expected" value, such as string or integer.

EDIT: Examples

$query = "INSERT INTO table (id, username, password) VALUES (NULL, '$_POST[username]', '".md5($_POST['password'])."')";
// This will be a correct query :)

$query = "INSERT INTO table (id, username, password) VALUES (1, '$_POST[username]', '".md5($_POST['password'])."')";
// This will be an incorrect query :(

$query = "INSERT INTO table (id, username, password) VALUES ('1', '$_POST[username]', '".md5($_POST['password'])."')";
// This will be a correct query :)