简体   繁体   English

PHP中的mysql_query错误-无法将信息插入数据库

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

I wrote this query in php: 我在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. 您必须用userColumnName的实际列名替换userColumnName等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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