无法使用php与PDO:: execute()插入数据库

时间:2022-09-26 08:25:52

I have created a simple mySQL database, and I am trying to insert some test data into it using PHP. When I ran the method on Firefox I got the following message, and I can not resolve this problem:

我创建了一个简单的mySQL数据库,并尝试使用PHP将一些测试数据插入其中。当我在Firefox上运行这个方法时,我得到了如下的信息,我无法解决这个问题:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number:

警告:PDOStatement:execute()[PDOStatement。执行]:SQLSTATE[HY093]:无效参数编号:

My method to insert sample into my datebase is:

我将样本插入我的数据库的方法是:

public function confirmInsert(){
    $admin="admin";
    $pass="12345v";
    $mail="admin@example.com";  
    $insertSQL = "INSERT INTO 'users' ('user_name', 'user_pass, 'user_email')
            VALUES (
                    :admin, 
                    :pass,
                    :mail)";
    try {

        $stmt = $this->db->prepare($insertSQL);
        $stmt ->bindParam(':user_name',$admin, PDO::PARAM_STR);
        $stmt ->bindParam(':user_pass', $pass, PDO::PARAM_STR);
        $stmt->bindParam(':user_email', $mail,PDO::PARAM_STR);
        $stmt->execute();
        $stmt->closeCursor();
        return TRUE;
        } catch (Exception $e) {
            $e -> getMessage();
        }
}

I am running on //localhost, and using apache 2.2 and php 5.2.17. Thanks!!

我正在//localhost上运行,并使用apache 2.2和php 5.2.17。谢谢! !

2 个解决方案

#1


3  

'You don't have the same name for your parameters in the query and when you bind them. You also have to remove the quotes around the fields name in the query.

“在查询和绑定参数时,您没有相同的名称。”您还必须删除查询中字段名周围的引号。

Should be better that way:

那样会更好:

public function confirmInsert(){
    $admin='admin';
    $pass='12345v';
    $mail='admin@example.com';  
    $insertSQL = "INSERT INTO users (user_name, user_pass, user_email)
            VALUES (
                    :admin, 
                    :pass,
                    :mail)";
    try {

        $stmt = $this->db->prepare($insertSQL);
        $stmt ->bindParam(':admin',$admin, PDO::PARAM_STR);
        $stmt ->bindParam(':pass', $pass, PDO::PARAM_STR);
        $stmt->bindParam(':mail', $mail,PDO::PARAM_STR);
        $stmt->execute();
        $stmt->closeCursor();
        return TRUE;
        } catch (Exception $e) {
            $e -> getMessage();
        }
}

#2


0  

I don't yet have the rights to make comments, so i have to post this as an answer..

我还没有权利发表意见,所以我得把这篇文章贴出来作为回答。

R.E. Pompom6784's answer, using bindParam() the variable name need not match the parameter:

R.E. pom6784的答案,使用bindParam()变量名不需要匹配参数:

$stmt->bindParam(':user_name', $admin, PDO::PARAM_STR);

Should work just fine.

应该工作的很好。

You are binding :user_name to accept only the variable named $admin

您正在绑定:user_name,只接受名为$admin的变量

..if that makes things a bit clearer.

. .如果那样的话,事情就会更清楚一些。

If you would like some more information on this topic, here's a good article to read through:

如果你想了解更多关于这个话题的信息,这里有一篇很好的文章供你阅读:

Why you Should be using PHP’s PDO for Database Access

为什么要使用PHP的PDO进行数据库访问

It includes examples where it is necessary to have matching variable and placeholder names.

它包含了需要具有匹配变量和占位符名称的示例。

#1


3  

'You don't have the same name for your parameters in the query and when you bind them. You also have to remove the quotes around the fields name in the query.

“在查询和绑定参数时,您没有相同的名称。”您还必须删除查询中字段名周围的引号。

Should be better that way:

那样会更好:

public function confirmInsert(){
    $admin='admin';
    $pass='12345v';
    $mail='admin@example.com';  
    $insertSQL = "INSERT INTO users (user_name, user_pass, user_email)
            VALUES (
                    :admin, 
                    :pass,
                    :mail)";
    try {

        $stmt = $this->db->prepare($insertSQL);
        $stmt ->bindParam(':admin',$admin, PDO::PARAM_STR);
        $stmt ->bindParam(':pass', $pass, PDO::PARAM_STR);
        $stmt->bindParam(':mail', $mail,PDO::PARAM_STR);
        $stmt->execute();
        $stmt->closeCursor();
        return TRUE;
        } catch (Exception $e) {
            $e -> getMessage();
        }
}

#2


0  

I don't yet have the rights to make comments, so i have to post this as an answer..

我还没有权利发表意见,所以我得把这篇文章贴出来作为回答。

R.E. Pompom6784's answer, using bindParam() the variable name need not match the parameter:

R.E. pom6784的答案,使用bindParam()变量名不需要匹配参数:

$stmt->bindParam(':user_name', $admin, PDO::PARAM_STR);

Should work just fine.

应该工作的很好。

You are binding :user_name to accept only the variable named $admin

您正在绑定:user_name,只接受名为$admin的变量

..if that makes things a bit clearer.

. .如果那样的话,事情就会更清楚一些。

If you would like some more information on this topic, here's a good article to read through:

如果你想了解更多关于这个话题的信息,这里有一篇很好的文章供你阅读:

Why you Should be using PHP’s PDO for Database Access

为什么要使用PHP的PDO进行数据库访问

It includes examples where it is necessary to have matching variable and placeholder names.

它包含了需要具有匹配变量和占位符名称的示例。