我正在创建网站民意调查,但出现此错误:
调用未定义的方法Poll :: updatePoll() 第10行上的C:\ xampp \ htdocs \ poll \ controllers \ poll.php
我已经检查了我的类定义和方法。一切似乎都很好,但错误不断出现。这是我模型中的代码:
<?php
class Poll {
private $db;
//method requires a database connection as argument
public function __construct( $dbConnection ) {
//store the received conection in the $this->db property\
$this->db = $dbConnection;
}
public function updatePoll( $input ) {
if ( $input==='yes') {
$updateSQL = "UPDATE poll SET yes = yes + 1 WHERE poll_id = 1";
} else if ( $input === 'no') {
$updateSQL = "UPDATE poll SET no = no + 1 WHERE poll_id = 1";
}
$updateStatement = $this->db->prepare($updateSQL);
$updateStatement->execute();
}
public function getPollData() {
//The actual SQL statement
$sql = "SELECT poll_question, yes, no FROM poll WHERE poll_id = 1";
//Use the PDO connection to create a PDOStatement object
$statement = $this->db->prepare($sql);
//tell MySQL to execute the statement
$statement->execute();
//retrieve the first row of data from the table
$pollData = $statement->fetchObject();
//make poll data available to the caller
return $pollData;
}
}
我的控制器中的代码如下:
<?php
include_once "models/poll.class.php";
$poll = new Poll( $db );
//check if the poll is submitted
$isPollSubmitted = isset($_POST['user-input']);
if ( $isPollSubmitted ) {
$input = $_POST['user-input'];
$poll->updatePoll($input);
}
$pollData = $poll->getPollData();
$pollView = include_once "views/poll-html.php";
return $pollView;
?>
这是我的索引文件中的代码。我怀疑$ db(数据库连接)可能有问题
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
//load model
include_once "models/Page_Data.class.php";
$PageData = new Page_Data;
$PageData->title = "PHP/MySQL site poll example";
$PageData->content = include_once "controllers/poll.php";
//database credentials
$dbInfo = "mysql:host=localhost;dbname=playground";
$dbUser = "root";
$dbPassword = "";
$db = new PDO( $dbInfo, $dbUser, $dbPassword);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$page = include_once "views/page.php";
//output generated page
echo $page;
?>