0

I have a simple system for a University Project which I have become stuck with an would really appreciate some help. I have created a 5 star rating system based on the user logged on, my project will all be running on localhost, but i need multiple people to test it on the same device.

My rating system works fine and inserts into my database with the ratings, however I wish to use my rating system across multiple pages. I have two files to complete my this action E1index.php and E1ajax.php which I have included.

I tried creating separate index files for my other pages (E2index.php, E3index.php etc ) so that the same user can rate multiple pages, but only 1 rate per user is inserting into my database, the second rate will overwrite the first.

My question is, can anyone explain how I can amend my code to have my rating insert into a separate row for the same user depending on the page they are on?

E1index.php

<?php  
    session_start();
    include_once 'dbconnect.php';

    if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array( $res );

        $post_id = '1'; 
    ?>

<!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>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-20" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="eNISATVids.php?home">Return to Tutorials page</a>&nbsp;&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
    <br>
<p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg">
<br>
  <link rel="stylesheet" href="style.css" type="text/css" />
        <link type="text/css" rel="stylesheet" href="css/example.css">


    <div class="tuto-cnt">

        <div class="rate-ex1-cnt">
            <div id="1" class="rate-btn-1 rate-btn"></div>
            <div id="2" class="rate-btn-2 rate-btn"></div>
            <div id="3" class="rate-btn-3 rate-btn"></div>
            <div id="4" class="rate-btn-4 rate-btn"></div>
            <div id="5" class="rate-btn-5 rate-btn"></div>
        </div>

        <hr>

        <div class="box-result-cnt">
            <?php
                $query = mysql_query("SELECT * FROM enisat_rate"); 
                while($data = mysql_fetch_assoc($query)){
                    $rate_db[] = $data;
                    $sum_rates[] = $data['rate'];
                }
                if(@count($rate_db)){
                    $rate_times = count($rate_db);
                    $sum_rates = array_sum($sum_rates);
                    $rate_value = $sum_rates/$rate_times;
                    $rate_bg = (($rate_value)/5)*100;
                }else{
                    $rate_times = 0;
                    $rate_value = 0;
                    $rate_bg = 0;
                }
            ?>
            <hr>
            <h3>This tutorial has been rated <strong><?php echo $rate_times; ?></strong> times.</h3>
            <hr>
            <h3>The average rating is <strong><?php echo $rate_value; ?></strong> stars.</h3>
            <hr>
            <div class="rate-result-cnt">
                <div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
                <div class="rate-stars"></div>
            </div>
            <hr>

        </div><!-- /rate-result-cnt -->

    </div><!-- /tuto-cnt -->


    <script>
        // rating script
        $(function(){ 
            $('.rate-btn').hover(function(){
                $('.rate-btn').removeClass('rate-btn-hover');
                var therate = $(this).attr('id');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-hover');
                };
            });

            $('.rate-btn').click(function(){    
                var therate = $(this).attr('id');
                var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
                $('.rate-btn').removeClass('rate-btn-active');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-active');
                };
                $.ajax({
                    type : "POST",
                    url : "http://localhost/Tna/E1ajax.php",
                    data: dataRate,
                    success:function(){}
                });

            });
        });
    </script>


</body>
</html>

E1ajax.php

<?php

session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

    if($_POST['act'] == 'rate'){
        //search if the userID has already gave a note
        $userID=$_SESSION['user'];
        $therate = $_POST['rate'];
        $thepost = $_POST['post_id'];

        $query = mysql_query("SELECT * FROM enisat_rate where user_id= '$userID'"); 
        while($data = mysql_fetch_assoc($query)){
            $rate_db[] = $data;
        }

        if(@count($rate_db) == 0 ){
            mysql_query("INSERT INTO enisat_rate (id_post, user_id, rate)VALUES('$thepost', '$userID', '$therate')");
        }else{
            mysql_query("UPDATE enisat_rate SET rate= '$therate' WHERE user_id = '$userID'");
        }
    } 
?>
scubbastevie
  • 37
  • 1
  • 13
  • University projects should really not be using the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Feb 20 '16 at 15:47
  • Also cannot think why you would be using the `@` error silencer on a `count()` – RiggsFolly Feb 20 '16 at 15:49
  • 1
    Hi @RiggsFolly, have you any suggestions about my question? – scubbastevie Feb 20 '16 at 16:05
  • Yea, create 10 different users, login as a different user each time and test it like that – RiggsFolly Feb 20 '16 at 16:17
  • Hi @RiggsFolly, I do have multiple users, that isnt the problem. If I log on as different users all their votes add up for a particular page, but if i create another page, i.e a different rating system, it still remembers the users vote for the first page and updates this. So any user can only vote once regardless of what page they are on. I tried changing the $post_id value on each index page but this didnt work, post_id changed in my table but still held their original vote, can you give any help with this it would be greatly appreciated, I am running out of time to finish my project – scubbastevie Feb 20 '16 at 20:14
  • Purely for the purposes of anyone reading this in future with a similar issue as me, I think a solution is to create a separate table for each page to store multiple rates for each user. This approach will be uneasy on the eye with a lot of pages and tables but it will work for this issue – scubbastevie Feb 20 '16 at 23:36
  • A better solution would be to to create a single table where the page name or some identifier was a column in the table – RiggsFolly Feb 21 '16 at 16:05
  • Hi @RiggsFolly, that was my original intention, but I dont know how to implement it, my code seems to be tailored to only allow one row per user, my pages would be E1index.php, E2index.php etc would you be able to offer a solution on how to amend the code already provided to reflect this if I created a column called "page" for example? Also my "post_id" already uniquely identifies each page, i was changing the value of it each time – scubbastevie Feb 21 '16 at 16:17

0 Answers0