1

嗨,我正在尝试读取我的对象 person 的实例,使用表单获取其个人详细信息,然后将其放入数据库中。我想我的代码设置了这个人,并连接到数据库好,我只是在处理表单信息并将其读入数据库时​​无法正确处理。任何帮助将不胜感激!

我的表格

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
       <form action="process.php" method="post">
        First Name: <input type="text" name="firstName" />
        Last Name: <input type="text" name="lastName" />
        Age: <input type="text" name="age" />
        <input type="submit" />
       </form>
    </body>
</html>

我尝试处理它的地方

   <?php
$i = 0;
$firstName = 'firstName';
$lastName = 'lastName';
$age = 'age';
$person = new person ($i,$firstName, $lastName, $age);
$PersonDAO = new PersonDAO();
$dao->insert($person);
?>

我的道

类 PersonDAO 扩展 Person{ protected $link;

public function __construct() {
    $host = "localhost";
    $database = "test";
    $username = "root";
    $password = "";

    $dsn = "mysql:host=$host;dbname=$database";

    $this->link = new PDO($dsn, $username, $password);
    $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
}
public function __destruct() {
    $this->link = null;
}

public function insert($person){
    if (!isset($person) && $person != null){
        throw new Exception("Person Required");
    }
    $sql = "INSERT INTO person(firstName, lastName, age)"
    . "VALUES (:firstName, :lastName, :age)";

    $params = array(
        'firstName' => $person->getFirstName(),
        'lastName' => $person->getLastName(),
        'age' => $person->getAge(),
    );

    $stmt = $this->link->prepare($sql);
    $status = $this->execute($params);
    if ($status != true){
        $errorInfo = $stmt->errorInfo();
        throw new Exception("Could Not Add Person: " . $errorInfo[2]);
    }

    $id = $this->link->lastInsertId('person');
    $person->setId($id);


    }

} ?>

我的表单很好,但是当我单击提交时,它显示“致命错误:在第 6 行的 /Applications/XAMPP/xamppfiles/htdocs/personProj/process.php 中找不到类 'person'”

有任何想法吗?谢谢

4

1 回答 1

1

您可能想尝试:

$person = new Person ($i,$firstName, $lastName, $age);

如果该类是用大写字母定义的,那么您也应该用大写字母来调用它。与方法/类调用的情况保持一致总是一个好主意。在许多其他语言(如 Java)中,这是非常严格的(尽管 PHP 在某些情况下可能不遵守此规则)。

于 2012-10-23T18:50:28.713 回答