我不断收到语法错误,我不知道出了什么问题。我不能调用一个预定的字符串吗?
$sqlstring= "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends)
VALUES (NULL , $email, $password, $name, CURDATE() , 0);";
我不断收到语法错误,我不知道出了什么问题。我不能调用一个预定的字符串吗?
$sqlstring= "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends)
VALUES (NULL , $email, $password, $name, CURDATE() , 0);";
如果$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
删除最后一个分号并用引号括起值。
$sqlstring= "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends)
VALUES (NULL , '$email', '$password', '$name', CURDATE() , 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)
);
您更新的查询是:
$sqlstring = "INSERT INTO friends (friend_id , friend_email , password , profile_name , date_started , num_of_friends) VALUES (NULL , '$email', '$password', '$name', CURDATE() , 0)";