意外的 '' (T_ENCAPSED_AND_WHITESPACE)、期望标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING) 错误
unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) error
在注册脚本中,错误不断弹出。代码如下:
try {
$dbh = new PDO("mysql:host=$hostname;dbname=booter", $username, $password);
echo 'Connected to database<br />';
/*** INSERT data ***/
$count = $dbh->exec("INSERT INTO members(username, password, email) VALUES ('$_POST['username']', '$hashedpassword', '$_POST['email']')");
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
对此问题的任何修复(以及有助于安全的任何提示)将不胜感激。
这一行有问题:
('$_POST['username']', '$hashedpassword', '$_POST['email']')
POST 数组中需要删除的引号。
但是,这根本不安全,您应该使用准备好的语句,因为它会让您面临 SQL injection。
首先将变量赋给POST个数组:
$username = $_POST['username'];
$hashedpassword = "The way you're getting this from";
$email = $_POST['email'];
然后使用占位符使用 ?
的预处理语句:
$query= "INSERT INTO members (username, password, email) VALUES (?, ?, ?)";
$result = $dbh->prepare($query);
$count = $result->execute(array($username, $hashedpassword, $email));
有关 PDO 准备语句的更多信息,请访问:
脚注:
我在您发布的另一个问题 中注意到您正在使用 mysqli_
函数。
如果您仍在使用 mysqli_
进行连接或 mysqli_
函数存在于您的代码中的其他地方,则您不能混合使用 MySQL API。
在注册脚本中,错误不断弹出。代码如下:
try {
$dbh = new PDO("mysql:host=$hostname;dbname=booter", $username, $password);
echo 'Connected to database<br />';
/*** INSERT data ***/
$count = $dbh->exec("INSERT INTO members(username, password, email) VALUES ('$_POST['username']', '$hashedpassword', '$_POST['email']')");
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
对此问题的任何修复(以及有助于安全的任何提示)将不胜感激。
这一行有问题:
('$_POST['username']', '$hashedpassword', '$_POST['email']')
POST 数组中需要删除的引号。
但是,这根本不安全,您应该使用准备好的语句,因为它会让您面临 SQL injection。
首先将变量赋给POST个数组:
$username = $_POST['username'];
$hashedpassword = "The way you're getting this from";
$email = $_POST['email'];
然后使用占位符使用 ?
的预处理语句:
$query= "INSERT INTO members (username, password, email) VALUES (?, ?, ?)";
$result = $dbh->prepare($query);
$count = $result->execute(array($username, $hashedpassword, $email));
有关 PDO 准备语句的更多信息,请访问:
脚注:
我在您发布的另一个问题 中注意到您正在使用 mysqli_
函数。
如果您仍在使用 mysqli_
进行连接或 mysqli_
函数存在于您的代码中的其他地方,则您不能混合使用 MySQL API。