Ok, well I'm trying to have a poll system so that when a visiter votes, a cookie is placed on their computer so that the next time they come, a new one will come up.
But, sometimes I get the following error:
getPoll fatal error: You have an error in your SQL syntax near '' at line 1
here's my code
<?php
session_start();
$maxNumOfPolls = 2;
// initialize pollnum if not set..
if (!$_SESSION["pollnum"]) {
$_SESSION["pollnum"] = 0;
}
if ($_SESSION["pollnum"] < $maxNumOfPolls) {
//set the poll selection for this script
$poll=1;
}
// Check for Post variables and transfer them to a script variable.
if (isset($HTTP_POST_VARS[vote]) )
$vote = $HTTP_POST_VARS[vote];
else
$vote=NULL;
// Connect to MySql and select the proper database
$DB_SERVER = "localhost";
$DB_USER = "XXXX";
$DB_PASS = "XXXX";
$DB_NAME = "vpoll";
$DB_TABLE = "data";
$link = mysql_pconnect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
$result = mysql_query("SELECT * FROM $DB_TABLE WHERE tid=1");
// If a vote has been registered update the count in the database
if ($vote) {
$qid = mysql_query("UPDATE data SET votes=votes+1 WHERE name='$vote' and tid='$poll'");
//incriment session cookie
$_SESSION["pollnum"]++;
if ( ! $qid )
die ( "getPoll fatal error: ".mysql_error() );
}
?>
<html>
<head>
<title>Poll</title>
<style type=text/css>
body { margin: 10px; background-color:#FFFFFF; font-family: Verdana; }
table { background-color:#E2E2E2; }
td.background { background-color: #000000; border:0px; }
td.barcolor { background-color: #FFFF00; border:0px; }
table.barcolor { background-color: #FFFF00; border:0px; }
td.text { font-size:8pt; }
td.question { font-size:10pt; color:#FFFFFF; font-weight:bold; text-align:center; background-color: #000000;}
</style>
</head><body>
<?php
$qid=mysql_db_query("vpoll","select * from topic where tid=$poll");
if ( ! $qid )
die ( "getPoll fatal error: ".mysql_error() );
else{
$text=mysql_fetch_row($qid);
mysql_free_result($qid);
}
$qid=mysql_db_query("vpoll","SELECT sum(votes) as sum FROM data WHERE tid=$poll");
if ( ! $qid )
die ( "getPoll fatal error: ".mysql_error() );
else{
$sum = mysql_result($qid,0,"sum");
mysql_free_result($qid);
}
$qid=mysql_db_query("vpoll","select * from data where tid=$poll order by votes DESC") or die("Database Query Error");;
if($vote){
print "<table border=0 width=150 class=\"background\"><tr><td class=\"question\" colspan=\"3\">$text[1]<br><br></td></tr><tr><td class=\"text\">$text[2]</td><td class=\"text\">Results</td><td class=\"text\">%</td></tr>\n";
while($row=mysql_fetch_row($qid)) {
print "<tr><td align=center></td>";
print "<td class=\"text\" colspan=\"2\"></td></tr><tr>
<td class=\"text\" align=\"left\">".$row[1]."</td><td class=\"text\">";
if($sum && $row[2]) {
$per = (int)(100 * $row[2]/$sum);
print "<table align=center border=0 cellspacing=0 cellpadding=1 width=\"80%\" height=\"10\">\n";
print " <tr>\n";
print " <td class=\"background\">\n";
print " <table align=left border=0 cellspacing=0 cellpadding=0 width=\"$per%\" height=\"100%\">\n";
print " <tr>\n";
print " <td class=\"barcolor\">\n";
print " <table class=\"barcolor\"><tr><td></td></tr></table>\n";
print " </td>\n";
print " </tr>\n";
print " </table>\n";
print " </td>\n";
print " </tr>\n";
print "</table>\n";
print"</td><td class=\"text\">$per</td>";
}
print "</tr>\n";
}
print "<tr><td class=\"text\" colspan=\"3\" align=\"center\"></td></tr>";
print "</table>\n";
}
else{
print "<form action=\"$PHP_SELF\" method=\"POST\">";
print "<table border=0 width=150><tr><td class=\"question\" colspan=\"3\">$text[1]<br><br></td></tr><tr><td class=\"text\" align=\"center\">Select</td><td class=\"text\">$text[2]</td><td class=\"text\"></td></tr>\n";
while($row=mysql_fetch_row($qid)) {
print "<tr><td align=center><input type=radio name=vote value=\"$row[1]\"></td>";
print "<td class=\"text\" colspan=\"2\">" .$row[1]."</td></tr><tr>
<td class=\"text\" align=\"center\"></td><td class=\"text\">";
if($sum && (int)$row[2]) {
$per = (int)(100 * $row[2]/$sum);
print"</td><td class=\"text\"></td>";
}
print "</tr>\n";
}
print "<tr><td class=\"text\" colspan=\"3\" align=\"center\"><input type=submit value=\"Submit\"></form></td></tr>";
print "</table>\n";
}
print "</body></html>";
?>