我正在努力为我的项目找到错误的解决方案,有人可以协助我并向我展示正确的方法吗?
这是我的代码:
$productkey = $_SESSION['key'];
$productuser = $_SESSION['user'];
$productemail = $_SESSION['email'];
// Include Database Connection
require 'assets/sys/config.php';
$query = dbConnect()->prepare("INSERT INTO _system (_product_key, _product_user, _product_email, _site_email, _site_title) VALUES (:_product_key, :_product_user, :_product_email, :_site_title, :site_email)");
$query->bindParam(':_product_key', $productkey);
$query->bindParam(':_product_user', $productuser);
$query->bindParam(':_product_email', $productemail);
$query->bindParam(':_site_title', $_POST['sitetitle']);
$query->bindParam(':_site_email', $_POST['siteemail']);
$query->execute();
我收到的错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in
在查询中,您有:
:site_email
但是您确实绑定了它:
$query->bindParam(':_site_email', $_POST['siteemail']);
这些是不同的,编辑:
$query->bindParam(':site_email', $_POST['siteemail']);
或者更改 sql 上的参数。
绑定参数中有拼写错误。 :site_email
而不是:_site_email
.
顺便一提。。。你知道你可以用一个简单的哈希数组来做到这一点,对吧?只需将键命名为与查询中的占位符相同的名称即可。
正如其他人指出的那样,有错别字。但是,您也有_site_email
和_site_title
顺序错误,因此您不会将数据插入到预期的位置。
应该是:
$query = dbConnect()->prepare("INSERT INTO _system (_product_key, _product_user, _product_email, _site_title, _site_email)
VALUES (:_product_key, :_product_user, :_product_email, :_site_title, :_site_email)");