Steve C.
Steve C.

Reputation: 1353

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

When I try to call the function "getAverage" within "storeRating", I get a HTML 500 server error. If I comment out that function, everything works perfectly. How can I call function "getAverage" withing function "storeRating"? Even if I leave that function uncommented, the code still checks for a duplicate rating and posts the new rating to the "rating" table. Please look at my code at the getAverage function. I need to be able to update the rating in the "products" table with the average.

Here are my PHP classes.

DB Functions:

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

Upvotes: 0

Views: 459

Answers (2)

Isky
Isky

Reputation: 1398

The problem is that you are calling getAverage() that is a method of yor class. So you need to $this, that is a reference to the current object, in order to call that function from your object. Changing your code to :

$this->getAverage()

will solve your problem.

Upvotes: 1

tillz
tillz

Reputation: 2108

You need to call $this->getAverage() Probably you should have a look at the PHP manual

Upvotes: 0

Related Questions