评级系统错误/ PHP mySQL

时间:2018-04-24 16:50:08

标签: php mysql rating

我正在尝试使用PHP和mySQL编写评级系统。到目前为止,我已经写了这个,一个非常简单的评级系统。我收到以下错误:

  

致命错误:未捕获错误:调用成员函数fetch()   布尔在/home/assign043/public_html/rate.php:33堆栈跟踪:#0   {main}在第33行的/home/assign043/public_html/rate.php中抛出

除错误外,数据库还没有更新平均评分,所以我觉得计算也有问题。

我还被要求使用特定的计算来计算平均评分:

new average rating = 
((previous average rating*nraters) + user’s rating) / (nraters+1)

这是代码: rate.php

    <?php
        $username = $_SESSION['solentuser'];
        echo "$username's account!";

        $conn=new PDO("mysql:host=localhost;dbname=user;","user","pass");

        $productID = $_GET['ID'];

        $result=$conn->query("SELECT * FROM products WHERE ID=$productID");


        while($row=$result->fetch()){
            $id = $row['ID'];
            $name = $row['name'];
            $manufacturer = $row['manufacturer'];
            $currentrating = $row['av_rating'];

            echo "<form action='rate2.php' method='POST'>";
            echo "Name: " . $row['name'] . "<br>";
            echo "Manufacturer: " . $row['manufacturer'] . "<br>";
            echo "Current Rating: " . $row['av_rating'] . "<br><br>";
            echo "Rate this product!<br>";
            echo "<select name='rating'>";
            echo "<option>1</option>";
            echo "<option>2</option>";
            echo "<option>3</option>";
            echo "<option>4</option>";
            echo "<option>5</option>";
            echo "</select>";
            echo "<input type='hidden' value='$id' name='id'>";
            echo "<input type='submit' value='Rate!'>";
            echo "</form>";

        }   
    ?>

rate2.php

    <?php

        $conn=new PDO("mysql:host=localhost;dbname=assign043;","assign043","eeThotev");

        $id = $_POST['id'];
        $newrating = $_POST['rating'];

        $result=$conn->query("SELECT * FROM products WHERE ID=$id");
        $row=$result->fetch();

        $new_raters= $num_raters + 1;
        $conn->query("UPDATE products SET num_raters = '$new_raters' WHERE ID=$id");

        $new_av_rating = (($av_rating*$num_raters) + $new_rating) / ($num_raters+1);

        $conn->query("UPDATE products SET av_rating = '$new_av_rating' WHERE ID=$id");

        header("location: rate.php");
    ?>

非常感谢所有帮助!我对此很陌生,并研究了类似的问题,但它们略有不同,我不知道如何将代码实现到我的代码中:(

0 个答案:

没有答案