0

我不断收到语法错误,我不知道出了什么问题。我不能调用一个预定的字符串吗?

$sqlstring= "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends)
                        VALUES (NULL , $email, $password, $name, CURDATE() , 0);";
4

4 回答 4

2

如果$email, $password, $name都是 varchar 或 string,则需要用单引号括起来。

$sqlstring= "INSERT INTO friends (friend_id , friend_email , password , 
                                 profile_name , date_started , num_of_friends)
            VALUES (NULL , '$email', '$password', '$name', CURDATE() , 0)";

您的查询是 vunerable SQL Injection,请花时间阅读下面的文章,以防止SQL Injction

于 2012-10-29T11:24:10.747 回答
1

删除最后一个分号并用引号括起值。

$sqlstring= "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends)
                    VALUES (NULL , '$email', '$password', '$name', CURDATE() , 0)";
于 2012-10-29T11:24:23.010 回答
0

如果您在查询中接受用户输入,那么简单地将其添加到您的 SQL 语句中是非常危险的。

如果您使用现代 PHP,您将使用 PDO 来准备您的语句...

$sth = $dbh->prepare('INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends)
                    VALUES (NULL , ?, ?, ?, CURDATE() , 0)');
$sth->execute(array($email, $password, $name));

或者,如果您想坚持老派,请避开它​​们:

$sqlstring = sprintf('INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends) VALUES (NULL , %s, %s, %s, CURDATE() , 0)',
    mysql_real_escape_string($email),
    mysql_real_escape_string($password),
    mysql_real_escape_string($name)
);
于 2012-10-29T11:31:06.807 回答
0

您更新的查询是:

$sqlstring = "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends) VALUES (NULL , '$email', '$password', '$name', CURDATE() , 0)";

于 2012-10-29T11:34:19.900 回答