• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Php mysql easy voting system.


I've made website that stores all my movies, games, apps and so on.

For example here's a the code for my dvd movies:

$amount = mysql_query("SELECT * FROM data WHERE category='dvd'");
$num_rows = mysql_num_rows($amount);

$querylatest = "SELECT date_format(max(timestamp), '%d.%m.%Y  %H:%i:%s') as date FROM data GROUP BY TIMESTAMP desc limit 1";
$resultlatest = mysql_query($querylatest) or die ("Error in query: $query. " . mysql_error());

while ($get_info = mysql_fetch_row($resultlatest))
     foreach ($get_info as $field)
             echo ("<p>Database last updated at:&nbsp;<br/><b>$field</b></p>\n");



echo "Total amount of Dvd:s: <b>$num_rows</b> pcs\n";

echo ("<table width=\"480\" bgcolor=\"#715e5e\" cellpadding=\"3\" cellspacing=\"2\" border=\"3\"\n>");
echo  ("<tr bgcolor=\"#723939\">");

echo ("<th style=\"color: white;\">Name</th>\n");
echo ("<th style=\"color: white;\">Category</th>");
echo ("<th style=\"color: white;\">Level</th>\n");
echo ("<th style=\"color: white;\">Status</th>\n");
echo ("<th style=\"color: white;\">Rating</th>\n");
echo ("<th style=\"color: white;\">Votes</th>\n");
echo ("<th style=\"color: white;\">Rate</th>\n");

echo  "</tr>";

$result = mysql_query("SELECT name, category, l1_number, status, who FROM data d, level_1 l WHERE  d.shelf_id=l.level_1_id and category='dvd' order by name limit $offset, $limit ");

      while ($row = mysql_fetch_assoc($result))
            echo ("<tr>");

            echo ("<td style=\"color: #ffffff;\"><a href=\"http://www.imdb.com/find?q=$row[name]\" target=\"_blank\"><font face=\"sans-serif, verdana, helvetica, arial;\" align=\"center\"></font>$row[name]</a></td>");
            echo ("<td style=\"color: #ffffff;\">$row[category]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[l1_number]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[status]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[rating]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[num_votes]</td>");
            echo ("<td style=\"color: #ffffff;\">
            <a href=\"main.php?id=content&page=rateit\">Rate</a></td>");

          echo ("</tr>");

      echo ("</table>");
      echo ("<br/>");

$endtime = microtime();

$diff = number_format(((substr($endtime,0,9)) + (substr($endtime,-10)) - (substr($starttime,0,9)) - (substr($starttime,-10))),4);

//Display links for pages
if ($page == 1)
      echo "<img style=\"border: none;\" src=\"pics/arrowp.gif\" alt=\"Previous page\"/>";

      echo "<a href=\"main.php?id=content&page=dvd&dvd_page=" . ($page - 1) . "\"><img style=\"border: none;\" src=\"pics/arrowp.gif\" alt=\"Previous page\"/></a>";

      for ($i = 1; $i <= $pager->numPages; $i++)
            echo " | ";
          if ($i == $pager->page)
                echo " $i";
                echo "<a href=\"main.php?id=content&page=dvd&dvd_page=$i\"> $i</a>";
      echo " | ";

      if ($page == $pager->numPages)
      echo "<img style=\"border: none;\" src=\"pics/arrow.gif\" alt=\"Next page\" />";

      echo "<a href=\"main.php?id=content&page=dvd&dvd_page=" . ($page + 1) . "\"><img style=\"border: none;\" src=\"pics/arrow.gif\" alt=\"Next page\" /></a>";

echo"<br><small><small>Search took $diff s </small></small>";


To have a look at it in realtime, here's the address:


user: testabc
pass: test123

What I would like to create is a voting system so that users can give their votes on various titles and then show the current voting points and the number of votes
in the same table where the movietitle is being shown.

It's been a while sins I played around with php.. and I never been good with it :(

Thanks for your help.

  • 2
  • 2
1 Solution
Khanh DoanSenior DeveloperCommented:
You will have to create another table : vote
table will be like this :
id | productid | vote

when user vote a product, it will insert into table by query
mysql_query("INSERT INTO vote (productid, vote) VALUES ('$productid', '$vote');");

where $productid is id of your product, you can get this value by using $_GET or $_POST

To caculate the average vote :

$query = mysql_query("SELECT * FROM vote WHERE productid='$productid'");
$nums = mysql_num_rows($query);

while ($vote = mysql_fetch_array($query))
     $total = $total + $vote['vote'];

$average_vote = $total / $num;
// To show number of vote, you only need echo $nums

ToubeAuthor Commented:
Thanks for your answer,

Hmm...don't know if I'm following you here.. so if a user votes for a product, do I have to create a form or how will the voting work?

Khanh DoanSenior DeveloperCommented:
Yes, you will have to create a form.

$vote = $_POST['vote'];
$productid = $_POST['pid'];
mysql_query("INSERT INTO vote (productid, vote) VALUES ('$productid', '$vote');");

<form name="vote" action="vote.php", method="post">
<select name="vote">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<input type="hidden" name="pid" value="1">

ToubeAuthor Commented:
Thanks will try it later:)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now