尝试将用户输入插入 MYSQL 数据库。我正在使用 REPLACE INTO
因为列 email
有一个唯一的键来防止重复。表名称为 launch_email
。我试图通过使用 prepare
和 bindParam
来防止 SQL 注入(inject),但是我不断收到此错误:调用未定义的函数 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;
}
最佳答案
请记住,bindParam 是 PDO、MySQLi 类或您正在使用的任何数据库类的方法...因此必须以这种方式调用它:
$replace->bindParam(":email_str", $email, PDO::PARAM_STR);
关于php - 尝试使用prepare和bindParam将用户数据插入SQL表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23203193/