简体   繁体   中英

Error with mysql_query in php - Can't insert information into database

I wrote this query in php:

$query = "insert into admin values($user,$pass,$email,$signature)";
mysql_query($query);

But it has problems and can't insert information in database. Why? The database doesn't have any mistakes.

You have to put string within quotes. Try in this way and check error messages.

$query = "insert into admin values('$user','$pass','$email','$signiture')";
mysql_query($query) or die(mysql_error());

You need to quote string values:

$query = "insert into admin values('$user','$pass','$email','$signiture')";

Also your values count must match your columns count EXACTLY or else you have to explicitly list your columns out:

$query = "insert into admin (`username`, `password`, `email`, `signiture`)  values('$user','$pass','$email','$signiture')";

Also "signiture" is mis-spelt.

For one, you need to put single quotes around your variables. In addition, you need to make sure that your variables are properly escaped because your query will still fail if one of your variables has a single quote.

$query = sprintf("insert into admin values('%s','%s','%s','%s')",
        mysql_real_escape_string($user),
        mysql_real_escape_string($pass),
        mysql_real_escape_string($email),
        mysql_real_escape_string($signiture));

mysql_query($query);

Finally, as other users have noted, it's generally a good idea to explicitly list the columns that you wish to insert.

you need to surround your values with single quote like this

$query = "insert into admin values('$user','$pass','$email','$signiture')";

If above does not work try to use the following syntax instead:

INSERT INTO TABLE(col1,col2,col3) VALUES('val1','val2','val3')

Try this (may not make a difference, but worth a shot):

$query = "insert into admin (userColumnName, passColumnName, emailColumnName, sigColumnName) values ($user, $pass, $email, $signature)";
mysql_query($query);

You'll have to replace userColumnName , etc with the actual column names in your DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM