Tman
Tman

Reputation: 3

Update database based on radio button value

Im trying to create a rating system for a website. Im able to retrieve the items from the database and display 5 radio buttons under each item. The 5 radio buttons have the values 1 to 5.

Im trying to update the rating of each item based on the value of the radio button thats selected.

I have the following code:

<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) { 
     $i=0;
     echo '<table><tr>';

     echo '<br/>';
     echo '<br/>';

     while($obj = $results->fetch_object())
    {   
        echo '<td>';
        echo '<div class="tvProgs">'; 
        echo '<form method="post" id = "programmes" action="">';
        echo "<input type=\"hidden\" name=\"progID\" value=\"".htmlentities($obj->ProgrammeID)."\" />";
        echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
        echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
        echo '<div class="progRating"><h4>'.$obj->Rating.'</h4></div>';
        echo '<input type="radio" class="rating-input" id="rate" name="rate" value="1">';
        echo '<input type="radio" class="rating-input" id="rate" name="rate" value="2">';
        echo '<input type="radio" class="rating-input" id="rate" name="rate" value="3">';
        echo '<input type="radio" class="rating-input" id="rate" name="rate" value="4">';
        echo '<input type="radio" class="rating-input" id="rate" name="rate" value="5">';
        echo '<br/>';
        echo '</form>';
        echo '</div>';
        echo '</td>';
        $i++; 
        if ($i == 5 OR $i == 10) {
          echo '</tr><tr>';
        }
    }
     echo '</tr></table>';
}



    if(isset($_POST['rate'])){
        $newRating = $_POST['rate'];
        $ID = $_POST['progID'];

        $upsql = "UPDATE programmes SET Rating = Rating + $newRating WHERE ProgrammeID='$ID'";
        $stmt = $mysqli->prepare($upsql);
        $stmt->execute();
    }

?>

All of the code works fine except for the Update statement where im adding to the current rating that is already stored in the database.

Can someone help me on how to fix this issue

Thanks!

Upvotes: 0

Views: 787

Answers (1)

miken32
miken32

Reputation: 42699

You need to be catching error conditions, this will tell you what, if anything, is wrong with your statement. Also you should be using prepared statements properly to avoid security problems. Finally, you should do your database insert before displaying the data, to ensure you're showing the latest data.

if(isset($_POST['rate'])){
    $upsql = "UPDATE programmes SET Rating = Rating + ? WHERE ProgrammeID = ?";
    $stmt = $mysqli->prepare($upsql);
    if (!$stmt) {
        echo "Error preparing: " . $mysqli->error . "<br/>";
    } else {
        $stmt->bind_param('is', $_POST['rate'], $_POST['progID']);
        if (!$stmt->execute()) {
            echo "Error executing: " . $mysqli->error . "<br/>";
        } else {
            echo $stmt->affected_rows . "rows affected<br/>";
        }
    }
}

$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) { 
...

Upvotes: 1

Related Questions