繁体   English   中英

PHP脚本返回:SQLSTATE [HY093]:无效的参数编号:参数未定义

[英]PHP script returns: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

我查看了其他一些本质上与我的问题类似的问题,但是我没有在此PDO数据库编写器脚本中使用数组或循环。 对于其他一些解决方案感到困惑,我仍然发现自己陷入了这个问题。

我的脚本是这样写的:

<?php
class DatabaseWriter
{
    public function writeUserToDatabase($email , $name , $age , $gender , $country , $category)
    {   
        $host = '***********';
        $dbname = 'emailcollection';
        $user = '**********';
        $pass = '**********';
        $jointime = '2013-01-07 11:19:08';

        try {
            $connection = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $user, $pass);
            $connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
            $statement = $connection -> prepare("INSERT INTO emailcollection (emailaddress, name, age, gender, country, category, jointime) VALUES (:emailaddress, :name, :age, :gender, :country, :category, :jointime)");
            $statement -> bindValue(':email', $email);
            $statement -> bindValue(':name', $name);
            $statement -> bindValue(':age', $age);
            $statement -> bindValue(':gender', $gender);
            $statement -> bindValue(':country', $country);
            $statement -> bindValue(':category', $category);
            $statement -> bindValue(':jointime', $jointime);
            $statement -> execute();

            $connection = NULL;
        }catch (PDOException $e){
            echo $e -> getMessage();
        }
    }
}

?>

输出:

SQLSTATE [HY093]:参数号无效:参数未定义

您可能想知道$ jointime是什么。 那是表中的最后一列,并且因为我收到的内容是“无效的参数编号”,所以我认为我应该完全匹配列数(我试图在写入用户时添加时间戳,在PHP或SQL中)。

感谢您的提前反馈。

解决方案是查询使用列名:emailaddress,而bindValue()引用:email。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM