By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

storing star ratings

P: 59
Hi,

I need to store the star rated values in mysql. How will I do it. This is the code I have used to create the star rater,

<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.5/5 Stars.</li>
<li><a href="#" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="#" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="#" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="#" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="#" title="5 stars out of 5" class="five-stars">5</a></li>
</ul>

and I need to get the rated values(like 1 star or 2 star) and store that in mysql using php. Once the user rates a picture for example 4 stars, how will I get that value into php variable and store it in mysql. I am totally new to this concept. Can anybody give me some idea.

With regards
Feb 13 '08 #1
Share this Question
Share on Google+
5 Replies


ronverdonk
Expert 2.5K+
P: 4,258
See this little example named A.PHP.
It gives assigns a HREF link to each star rating. When the user clicks it will call itself passing the choosen rating number. In the second pass you can pick up that rating from the $_GET array and store it in the database.
NOTE!!! in order to prevent any attacks you must cleans and verify the validity of the input before storing it in the database!!
[php]<?php
// Script A.PHP

if (isset($_GET['stars'])) {
$stars=strip_tags(htmlentities($_GET['stars']));
// NOTE!!! You must cleanse and check the input before storing in the database
//
// From here you can store the value in your db
}
else {
?>
<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.5/5 Stars.</li>
<li><a href="a.php?stars=1" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="a.php?stars=2" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="a.php?stars=3" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="a.php?stars=4" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="a.php?stars=5" title="5 stars out of 5" class="five-stars">5</a></li>
</ul>
<?php }
?>[/php]
Ronald
Feb 13 '08 #2

P: 59
See this little example named A.PHP.
It gives assigns a HREF link to each star rating. When the user clicks it will call itself passing the choosen rating number. In the second pass you can pick up that rating from the $_GET array and store it in the database.
NOTE!!! in order to prevent any attacks you must cleans and verify the validity of the input before storing it in the database!!
[php]<?php
// Script A.PHP

if (isset($_GET['stars'])) {
$stars=strip_tags(htmlentities($_GET['stars']));
// NOTE!!! You must cleanse and check the input before storing in the database
//
// From here you can store the value in your db
}
else {
?>
<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.5/5 Stars.</li>
<li><a href="a.php?stars=1" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="a.php?stars=2" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="a.php?stars=3" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="a.php?stars=4" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="a.php?stars=5" title="5 stars out of 5" class="five-stars">5</a></li>
</ul>
<?php }
?>[/php]
Ronald

Hi,
thanks for your solution. It is working, but there is some problem.
Since I am linking to same page a.php, when I click the stars it will take me to the blank page. I am using this rating star in a form where the user has to fill the next contents, but as soon as the star is clicked it directs to blank page.
How to solve this issue.

With regards
Feb 14 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Of course it gives you a blank page, that code was not complete, it was just a sample on how to do it!

You are supposed to complete the code yourself within this piece
[php]if (isset($_GET['stars'])) {
$stars=strip_tags(htmlentities($_GET['stars']));
// NOTE!!! You must cleanse and check the input before storing in the database
//
// From here you can store the value in your db
}[/php]
Ronald
Feb 14 '08 #4

P: 59
Thank you

With Regards
Feb 14 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
Thank you

With Regards
You are welcome any time.

Ronald
Feb 14 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.