尝试将用户输入插入MYSQL数据库。我正在使用REPLACE INTO
,因为列email
具有唯一的密钥以防止重复。表名为launch_email
。我正在尝试使用prepare
和bindParam
来阻止SQL注入,但我不断收到此错误:Call to undefined function bindParam()
。任何解决方案?
PHP / SQL:
require(ROOT_PATH . "inc/database.php");
try {
$replace = $db->prepare("REPLACE INTO launch_email VALUES (:email_str)");
$replace = bindParam(":email_str", $email, PDO::PARAM_STR);
$replace->execute();
} catch (Exception $e) {
echo "Data could not be submitted to the database.";
exit;
}
编辑:下面的代码解决了我的问题。我正在为非对象分配方法。
require(ROOT_PATH . "inc/database.php");
try {
$replace = $db->prepare("REPLACE INTO launch_email VALUES (:email_str)");
$replace->bindParam(":email_str", $email, PDO::PARAM_STR);
$replace->execute();
} catch (Exception $e) {
echo "Data could not be submitted to the database.";
exit;
}
答案 0 :(得分:2)
记住bindParam是一个类PDO,MySQLi或你正在使用的任何数据库的方法......所以它必须这样调用:
$replace->bindParam(":email_str", $email, PDO::PARAM_STR);
答案 1 :(得分:0)
PDOStatement::bindParam是一种PDOStatement方法。
这应该有效
require(ROOT_PATH . "inc/database.php");
try {
$stm = $db->prepare("REPLACE INTO launch_email VALUES (:email_str)");
$stm->bindParam(":email_str", $email, PDO::PARAM_STR);
$stm->execute();
} catch (Exception $e) {
echo "Data could not be submitted to the database.";
exit;
}
答案 2 :(得分:0)
bindParam
不是语言功能。这是method of the PDOStatement object。
$replace->bindParam(":email_str", $email, PDO::PARAM_STR);
最好不要使用$replace
作为变量名称,而是将其称为$stmt
,以便更明显地说明该对象是什么。
或者,您可以在执行时传递参数。通过这样编写代码:
$stmt = $db->prepare("REPLACE INTO launch_email VALUES (?)");
if ($stmt)
{
// Execute query with parameter
$stmt->execute(array($email));
}
else
{
// Could not prepare statement
echo $db->errorInfo();
}