Simple like/dislike rating system in php and MySQL

久未见 提交于 2019-12-21 05:41:31

问题


I want to add a simple rating system to my random video site (id = youtube id)
I don't have much experience with php and MySQL and I'm not sure how to update a field using submit buttons in this way:

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name"rateform">
  <input name="rateup" type="image" src="up.png" id="rateup" value="rateup" />
  <input name="ratedown" type="image" src="down.png" id="ratedown" 
   value="ratedown" />
</form>
<?PHP
mysql_connect(",",",",",")or die(mysql_error());
mysql_select_db(",")or die(mysql_error());
if ($_POST['rateup'])
{
    mysql_query("UPDATE utube SET rating = rating + 1 
                WHERE (id = $pageid)");} else if ($_POST['ratedown']) {
    mysql_query("UPDATE utube SET rating = rating - 1 
                WHERE (id = $pageid)");}

?>

Is there something I have to do to link the html and php together?
All of the statements return the correct values by themselves (i.e $pageid)
but when I press the buttons there is nothing happening to any fields.

When I put the mysql query directly into phpmyadmin it also works,
I'm just not sure about how the html communicates with the php?
I'd appreciate if someone were to inform me of how this works so I can get my script to work.


回答1:


Let's start finding the problem: I can only imagine two reasons for this:

  • PHP is not connecting to the DB. Try executing the query directly from your script (taking it out of the if statement.
  • The if statement is wrong for some reason: Try replacing the mysql_query with print('up'); and print('down');

By the way, else if is a one-word-statement. You can replace it with elseif.




回答2:


Image buttons post clicked coordinate value except form name. inputname_x & inputname_y

if ($_POST['rateup_x'])
{
    mysql_query("UPDATE utube SET rating = rating + 1 
      WHERE (id = $pageid)");} else if ($_POST['ratedown_x']) {
    mysql_query("UPDATE utube SET rating = rating - 1 
      WHERE (id = $pageid)");}



回答3:


<?PHP
mysql_connect("hostname","username","password")or die(mysql_error());
mysql_select_db("dbname")or die(mysql_error());
if ($_POST['rateup'])
{
    mysql_query("UPDATE utube SET rating = rating + 1 
                WHERE (id = $pageid)");} else if ($_POST['ratedown']) {
    mysql_query("UPDATE utube SET rating = rating - 1 
                WHERE (id = $pageid)");}

?>


来源:https://stackoverflow.com/questions/5820158/simple-like-dislike-rating-system-in-php-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!