php - 如何使用 PHP 在另一个函数中调用此 PDO 函数?

标签 php mysql pdo

当我尝试在 "storeRating" 中调用函数 "getAverage" 时,出现 HTML 500 服务器错误。如果我注释掉该函数,一切都会完美运行。如何使用函数 "storeRating" 调用函数 "getAverage"?即使我不注释该函数,代码仍然会检查重复的评级并将新的评级发布到“评级”表中。请查看我的 getAverage 函数代码。我需要能够用平均值更新“产品”表中的评级。

这是我的 PHP 类(class)。

数据库功能:

    <?php


class DB_TestFunctions {

    private $conn;

    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $db = new Db_Connect();
        $this->conn = $db->connect();
    }

    // destructor
    function __destruct() {

    }

    // Storing new rating
    public function storeRating($pid, $userid, $ratingpnt) {

        $stmt = $this->conn->prepare("INSERT INTO rating(ProductID,UserID,prod_rating) VALUES(?, ?, ?)");
        $stmt->bind_param("sss", $pid, $userid, $ratingpnt);
        $result = $stmt->execute();
        $stmt->close();

        getAverage($pid);

        // check for successful store
        /* if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM products WHERE pid = ?");
            $stmt->bind_param("s", $pid);
            $stmt->execute();
            $rating = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            return $rating;
        } else {
            return false;
        } */
    }

    /**
     * Check if rating exists
     */
    public function checkDuplicate($pid, $userid) {
        $stmt = $this->conn->prepare("SELECT prod_rating from rating WHERE ProductID = ? AND UserID = ?");

        $stmt->bind_param("ss", $pid, $userid);

        $stmt->execute();

        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // user existed 
            $stmt->close();
            return true;
        } else {
            // user not existed
            $stmt->close();
            return false;
        }
    }

    public function getAverage($pid){
        $stmt = $this->conn->prepare("UPDATE products SET prod_rating = (SELECT AVG(prod_rating) FROM rating WHERE ProductID = ?) WHERE pid = ?");

        $stmt->bind_param("s", $pid);

        $stmt->execute();

        $stmt->close();

    }

    public function getNewRating($pid){
        $stmt = $this->conn->prepare("SELECT * FROM products WHERE pid = ?");

        $stmt->bind_param("s", $pid);

        $stmt->execute();

        $rating = $stmt->get_result()->fetch_assoc();

        $stmt->close();

        return $rating;

    }

}

?>

发布率

    <?php

require_once 'include/DB_TestFunctions.php';
$db = new DB_TestFunctions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['pid']) && isset($_POST['userid']) && isset($_POST['rating'])) {

    // receiving the post params
    $pid = $_POST['pid'];
    $userid = $_POST['userid'];
    $rating = $_POST['rating'];

    // check if user already rated product
    if ($db->checkDuplicate($pid, $userid)) {
        // user already rated this product
        $response["error"] = TRUE;
        $response["error_msg"] = "Rating already exists." ;
        echo json_encode($response);
    } else {
        $db->storeRating($pid, $userid, $rating);

        // get new rating
        $rating = $db->getNewRating($pid);
        if ($rating) {
            // Rating successful
            $response["error"] = FALSE;
            $response["prod_rating"] = $rating["prod_rating"];
            echo json_encode($response);
        } else {
            // Rating failed
            $response["error"] = TRUE;
            $response["error_msg"] = "Unknown error occurred in posting rating!";
            echo json_encode($response);
        }
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters (pid, userid or rating) are missing!";
    echo json_encode($response);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Post Rating</title>
</head>
<body>
    <h1>Add Comment</h1> 
        <form action="postRate.php" method="post"> 
            Product ID:<br /> 
            <input type="text" name="pid" placeholder="Product ID" /> 
            <br /><br /> 
            Userid:<br /> 
            <input type="text" name="userid" placeholder="Username" /> 
            <br /><br />
            Rating:<br /> 
            <input type="text" name="rating" placeholder="rating" /> 
            <br /><br />
            <input type="submit" value="Rate" /> 
        </form>
</body>
</html>

最佳答案

问题是您正在调用 getAverage() 这是您的类的方法。 因此,您需要 $this,它是对当前对象的引用,以便从您的对象调用该函数。 将您的代码更改为:

$this->getAverage()

将解决您的问题。

关于php - 如何使用 PHP 在另一个函数中调用此 PDO 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36472642/

相关文章:

php - 了解 PHP 中奇怪的引用/循环行为

java - 同样的老故事 : Tomcat DBCP + MySQL, MySQLNonTransientConnectionException:连接关闭后不允许操作

mysql - 显示 "users"表中的所有用户名

php - 如何获取 WordPress 查询受影响的行数?

php - 保存实体学说/symfony2 的副本

mysql - Kohana ORM 按日期选择记录

mysql - 从数据库(mysql)中获取数据并将其显示在cakephp的文本框中

c# - .NET 类似于 PHP 的 PDO?

php - 参数号无效 : parameter was not defined error

php - 解释 PHP 接口(interface)