2016-04-07 14 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

を見ている必要があり$this->getAverage() を呼び出す必要がありますよあなたはyorクラスのメソッドであるgetAverage()を呼び出しています。 オブジェクトからその関数を呼び出すには、現在のオブジェクトへの参照である$ thisが必要です。 コードを変更する:

$this->getAverage() 

あなたの問題を解決します。

0

はおそらく、あなたは問題があることであるPHP manual

+0

これは答えではなく、あなたが書いたことはただのコメントで言われました。 – Mattia

+0

@Mattia申し訳ありませんが、これを忘れたことがあります。しかし、あなたのコメントは答えになるはずです。あなたがあなたのコメントを回答として投稿すると、私はそれを削除します。 – tillz

+0

問題ありません。ちょうどそれを言う – Mattia

関連する問題