2016-04-07 62 views
0

当我尝试调用功能“getAverage”“storeRating”,我得到一个HTML 500服务器错误。如果我评论这个功能,一切都完美无缺。我该如何调用函数“getAverage” withing函数“storeRating”?即使我没有评论该功能,代码仍会检查重复的评级,并将新评级发布到“评级”表。请在getAverage函数中查看我的代码。我需要能够使用平均值更新“产品”表中的评分。如何在使用PHP的另一个函数中调用此PDO函数?

这是我的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> 
+0

检查你的apache error.log以查看出了什么问题。 – Mattia

+1

将'$ stmt-> bind_param(“s”,$ pid);'改为'$ stmt-> bind_param(“ss”,$ pid,$ pid);在'getAverage()'内部并检查 ' – Saty

+0

@Mattia我在Apache中遇到这个错误:致命错误:调用第29行DB_TestFunctions中的未定义函数getAverage() –

回答

1

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

$this->getAverage() 

将解决您的问题。

0

你需要调用$this->getAverage() 也许你应该看看PHP manual

+0

这不是一个答案,你写的只是在评论中说的。 – Mattia

+0

@Mattia对不起,一定错过了这个。不过,我认为你的评论应该是一个答案。如果您将评论发布为答案,我会将其删除。 – tillz

+0

没问题。只是说出来 – Mattia

相关问题