Simple like/dislike rating system in php and MySQL

2019-03-30 21:32发布

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.

3条回答
孤傲高冷的网名
2楼-- · 2019-03-30 21:54

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楼-- · 2019-03-30 22:14

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.

查看更多
女痞
4楼-- · 2019-03-30 22:17
<?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)");}

?>
查看更多
登录 后发表回答