Php 在pdo pql语句中使用对象属性值

Php 在pdo pql语句中使用对象属性值,php,mysql,pdo,Php,Mysql,Pdo,我对这个很陌生。我希望有人能告诉我为什么下面的代码不能工作 我试图将Ajax请求中的数据发布到php脚本中。我正在加载中的值作为“用户”对象的属性。我希望使用它们来调用数据库函数,但我不明白为什么不能在PDO语句中使用对象属性值。他们来了没有定义 谢谢 <?php class Userclass{ public $id; public $forename; public $surname; public $greeting = "Mr";

我对这个很陌生。我希望有人能告诉我为什么下面的代码不能工作

我试图将Ajax请求中的数据发布到php脚本中。我正在加载中的值作为“用户”对象的属性。我希望使用它们来调用数据库函数,但我不明白为什么不能在PDO语句中使用对象属性值。他们来了没有定义

谢谢

    <?php

class Userclass{
    public $id;
    public $forename;
    public $surname;
    public $greeting = "Mr";
    public function f(){
        call_user_func("dbinsert");
        }
};


try {
    $user = new Userclass;
    $user->id = $_POST["id"];
    $user->forename = $_POST["forename"];
    $user->surname = $_POST["surname"];
    $user->f();
} catch (Exception $e) {
    echo "failed";
    exit;
}


function dbinsert(){
    Require("dbconnect.php");

        try {
            $sql = "INSERT INTO testtable (id, forename, surname)
                VALUES (:id, :forename, :surname)";    
            $insert = $db->prepare($sql);
            $insert->bindValue(':id', $user->id);
            $insert->bindValue(':forename', $user->forename);
            $insert->bindValue(':surname', $user->surname);
            $insert->execute();
            //$insert->execute(array(':id' => $user->id, ':forename' => $user->forename, ':surname' => $user->surname));
            //$insert->execute(array(':id' => "16", ':forename' => "Bill", ':surname' => "Gates"));
            echo $user->forename . " sucessfully added to database";

        } catch (Exception $e) {
            echo "Database insertion failed" . var_dump($e);
        };
}

?>

您的$user变量未在dbinsert()作用域中定义。您可以将dbinsert()函数添加到userclass。或者改为将$user设置为dbinsert()的参数



您是否尝试将try/catch对象放入函数中?也许这是一个范围问题,您在哪里定义了dbInsert函数中的
$user
?这是一个非常新的自学型函数,我可以麻烦您进一步提出建议吗?只是想更好地理解事情。。。如果我在类中有很多函数,以及该类的很多实例化,我假设它不会产生比在类中由一个函数调用的单独函数更重的负载?谢谢当然,你可能不会马上得到回复PI还在“dbinsert”函数中添加了“global$user;”。所以我现在有两种方法。在我玩过构建我的应用程序的其他部分之后,我会找出哪一个可能是最好的。非常感谢大家的帮助。关于执行时间;老实说,这取决于你处理的对象的数量以及你将要做的动作。创建一个用户对象并像本例中那样存储它并不需要担心。很好,感谢您的时间和建议!
   <?php
Require("dbconnect.php");

class Userclass{
    public $id;
    public $forename;
    public $surname;
    public $greeting = "Mr";

    public function dbinsert(){


        try {
            $sql = "INSERT INTO testtable (id, forename, surname)
                VALUES (:id, :forename, :surname)";    
            $insert = $db->prepare($sql);
            $insert->bindValue(':id', $this->id);
            $insert->bindValue(':forename', $this->forename);
            $insert->bindValue(':surname', $this->surname);
            $insert->execute();
            echo $this->forename . " sucessfully added to database";

        } catch (Exception $e) {
            echo "Database insertion failed" . var_dump($e);
        };
    }
};


try {
    $user = new Userclass;
    $user->id = $_POST["id"];
    $user->forename = $_POST["forename"];
    $user->surname = $_POST["surname"];
    $user->dbinsert();
} catch (Exception $e) {
    echo "failed";
    exit;
}

?>