1

I'm trying to implement a simple rating system in PHP using JavaScript, jQuery and MySQL. The code below returns a success message that the rating was successful (the query was successful) but it does not populate the table.

The "rating" table has 4 fields. int field for ratingID (auto incremented), char field for userID, char field for adventureID and an int field for ratingScore. I've been debugging this for about 35 minutes now and for the life of me can't find the issue with the code.

Submission form:

            <div class="stars">
                <form role="form" method="post">
                    <input class="star star-5" id="star-5" type="radio" name="star" val="5" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
                    <label class="star star-5" for="star-5"></label>
                    <input class="star star-4" id="star-4" type="radio" name="star" val="4" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
                    <label class="star star-4" for="star-4"></label>
                    <input class="star star-3" id="star-3" type="radio" name="star" val="3" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
                    <label class="star star-3" for="star-3"></label>
                    <input class="star star-2" id="star-2" type="radio" name="star" val="2" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
                    <label class="star star-2" for="star-2"></label>
                    <input class="star star-1" id="star-1" type="radio" name="star" val="1" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
                    <label class="star star-1" for="star-1"></label>
                </form>
            </div>

JavaScript file:

var rating = {
    rating: 0,
    userID: "",
    adventureID: "",
};

function rate(userID, adventureID) {
    if(rating.rating = document.getElementById('star-5').checked)
        rating.rating = document.getElementById('star-5').value;
    else if(rating.rating = document.getElementById('star-4').checked)
        rating.rating = document.getElementById('star-4').value;
    else if(rating.rating = document.getElementById('star-3').checked)
        rating.rating = document.getElementById('star-3').value;
    else if(rating.rating = document.getElementById('star-2').checked)
        rating.rating = document.getElementById('star-2').value;
    else if(rating.rating = document.getElementById('star-1').checked)
        rating.rating = document.getElementById('star-1').value;
    rating.userID = userID;
    rating.adventureID = adventureID;
    callPHPrating();
}

function callPHPrating() {
    // call ajax
    $.ajax({
        type: "POST",
        url: 'rating_submit.php',
        data:{rating:rating.rating, userID:rating.userID, adventureID:rating.adventureID},
        success:function(msg) {
            alert(msg);
            if(msg == "Rating submitted.")
                window.location.reload();
            //Reload page if successful
        }
    });
}

rating_submit.php file:

<?php
    $ratingInput = $_POST['rating'];
    $userid = $_POST['userID'];
    $adventureid = $_POST['adventureID'];
    $cfg = include('utilities.php');

    $con = mysqli_connect($cfg['host'], $cfg['user'], $cfg['password'], $cfg['database']);
    $query = "INSERT INTO rating (userID, adventureID, ratingScore) VALUES('$userid', '$adventureid', '$ratingInput')";
    mysqli_query($con, $query);
    if(!$query)
        die('Invalid enquiry '.mysqli_error());
    else echo "Rating submitted.";
?>
hoeRIZON
  • 59
  • 6
  • Is PHP throwing an error or are you just not seeing the database being populated? – Enijar Dec 16 '15 at 22:33
  • PHP isn't throwing any errors. Database isn't being populated. – hoeRIZON Dec 16 '15 at 22:34
  • Are all `adventureID` values coming back as integers? I see you have defined them as `varchars`. – Enijar Dec 16 '15 at 22:38
  • adventureID and userID is auto incremented in the database by default as well. I am using the same method for comment, post and user profile functionality. That's why I'm posting here. Literally nearly identical code which, in theory, SHOULD work, but doesn't. – hoeRIZON Dec 16 '15 at 22:40
  • 1
    You should be using the `==` operator (comparison operator) instead of `=` (assignment operator) in your `if` statements. – Dave Dec 16 '15 at 22:40
  • @dave that's a good point. I don't even need to use the '==' operation as I'm checking if the radio button is checked. – hoeRIZON Dec 16 '15 at 22:41
  • @dave No he doesn't, you can assign values and check if they are `checked` like this in JavaScript. See this [**JSFiddle**](https://jsfiddle.net/Lba5wa0L/). – Enijar Dec 16 '15 at 22:43
  • Changed it to `if(document.getElementById('star-5').checked)` and etc. but it still doesn't work. – hoeRIZON Dec 16 '15 at 22:43
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Jay Blanchard Dec 16 '15 at 22:45
  • @JayBlanchard I'm not bothered about this at the moment. I want to get it to work first. – hoeRIZON Dec 16 '15 at 22:45
  • If you have phpmyadmin installed you can echo out your query, copy it into the SQL page, and it will tell you what the error is. – cantelope Dec 16 '15 at 22:46
  • @cantelope for some reason instead of an integer for the rating the query tries to push through 'on'. Any ideas why this happens? – hoeRIZON Dec 16 '15 at 22:50
  • javascript is interpreting document.getElementById('star-1').checked as "on" or "off". – cantelope Dec 16 '15 at 22:53
  • So even though I'm trying to return a value it just returns "on" or "off" depending if it's checked? Weird. I'll just use `if(document.getElementById('star-5').checked) rating.rating = 5;` and etc. This worked. Thank you so much for the help. Can't set an answer as every answer was a comment. – hoeRIZON Dec 16 '15 at 22:55

0 Answers0