如何使用PHP在另一个函数中调用这个PDO函数


How can I call this PDO function within another function using PHP?

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

以下是我的PHP类

DB功能:

    <?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;
    }
}
?>

postRate

    <?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(),它是yor类的一个方法。因此,您需要$this,这是对当前对象的引用,以便从对象中调用该函数。将您的代码更改为:

$this->getAverage()

会解决你的问题。

您需要调用$this->getAverage()也许您应该看看PHP手册