
This is a basic script using PHP MySQL, you can read advance rating script using PHP MySQL & Ajax here
There are Three parts in this scirpt.$cont_id="1";// unique id for the page
require "config.php"; // Database connection
require "rating.php";// Show the rating buttons ( Part I )
require "ratingck.php";// handle the rating submitted ( Part II
require "disp.php";// Display the rating already submitted ( part III)
<?Php
echo "<TABLE width=95% border=0 align=center cellpadding=0 cellspacing=0 >
<form name=f1 action='' method=post>";
<input type=hidden name=cont_id value='$cont_id'>
<input type=hidden name=todo value='submit-rating'>
<tr><td ><INPUT TYPE=RADIO NAME=rone Value=1 onClick='f1.submit()';><img src=images/star.gif>1</td>
<td><INPUT TYPE=RADIO NAME=rone Value=2 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif>2</td>
<td ><INPUT TYPE=RADIO NAME=rone Value=3 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>3</td>
<td ><INPUT TYPE=RADIO NAME=rone Value=4 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>
<img src=images/star.gif>4</td>
<td ><INPUT TYPE=RADIO NAME=rone Value=5 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif>
<img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>5</td></tr>
<tr><td align=center colspan=5 bgcolor='#f1f1f1'> <INPUT TYPE=SUBMIT value=Vote NAME=Vote>
</td> </tr></form></table></center> ";
?>
<?Php
@$todo=$_POST['todo'];
if(isset($todo) and $todo=="submit-rating"){
$rone=$_POST['rone']; // Collect the rating value
$msg="";
$status="OK"; // Flag to store status
if(!isset($rone)){$msg=$msg."Pleae give your score and then click the button";
$status="NOT OK";
}
// If required additional validations can be added here //
if ($status=="OK"){
if($stmt = $connection->prepare("SELECT rating,nov FROM content_rating WHERE content_rating.cont_id=?")){
$stmt->bind_param('i',$cont_id);
$stmt->execute();
$result = $stmt->get_result();
$row=$result->fetch_object(); // get data from table for the page
}else{
echo $connection->error;
}
if($result->num_rows==0){
// This is a new page so first add a new record
$query="INSERT INTO content_rating (cont_id,nov,rating) values ( ?,1,?)";
$stmt=$connection->prepare($query);
if($stmt){
$stmt->bind_param("ii", $cont_id, $rone);
if($stmt->execute()){
echo "No of records inserted : ".$connection->affected_rows; // Confirming the update
}else{
echo $connection->error;
}
} // end if , if query execution is successful
} // end if , no record for the rating is found for the page.
else { // Update the rating for the record
$rating=$row->rating;// read previous rating available in table
$nov=$row->nov + 1; // Increase number of votes by 1
$status="OK"; // set the flat
$rating=$rating+$rone; // Add current rating to stored rating
$score_avg=number_format(($row->rating/$row->nov),2,".",""); // Find previous rating to display
$stmt = $connection->prepare("UPDATE content_rating SET rating=$rating,nov=$nov WHERE cont_id =?");
if ($stmt) {
$stmt->bind_param('i', $cont_id);
$stmt->execute();
//echo "Record Updated:";
echo "Averate rating : $score_avg"; // Display the rating
//echo $stmt->affected_rows;
}else{
echo $connection->error;
}
}
}else{echo $msg;}
}// end of todo checking
?>
Read the part II for display of average rating
<?Php
### Using SQLite database #####
// This will create the database if not exists in the same location where the script is running.
// For a different location give the path details. // use any one line below.
//$my_conn = new PDO('sqlite:D:\\sqlite-data\\rating.db');// different path
//$my_conn = new PDO('sqlite:'.dirname(__FILE__).'/rating.db'); // same location
//$my_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
#### End of SQLite database ####
###### PHP PDO ##### For php PDO use below lines ##
$host_name = "localhost"; // or different host
$database = "my_db"; // Change your database name
$username = "root"; // Your database user id
$password = "password";// Your password
//////// Do not Edit below /////////
try {
$my_conn = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
####### END of PHP PDO ######
?>
Scripts
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.