problem creating poll using mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

problem creating poll using mysql

Post by mike08 »

i keep getting this error

any ideas?


You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND voteip='127.0.0.1'' at line 1

- someone else said this

your variable $poll_id is empty. try filling in a number for it.

how do i do that can you explain please?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Post the $sql/$query or whatever the line is, that makes this line fubar please. We need to see the whole picture to resolve this error.
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

code posted

Post by mike08 »

this is the code
think the error is in the select statement.

Code: Select all

$pollid=$_GET["pollid"];
$vote=$_POST["vote"];

$datestamp=date("Y-m-d H:i:s", time());
$ip=getenv("REMOTE_ADDR");
$ref=getenv("HTTP_REFERER");
$ref=explode("?", $ref);

$sql_vote="SELECT * FROM pollstats WHERE pollid=$pollid AND voteip='$ip'";
$res_vote=mysql_query($sql_vote) or die(mysql_error());
$num_vote=mysql_num_rows($res_vote);

if (!$vote) {
$url=$ref[0]."?pollerr=Something+went+wrong+with+your+vote.";
header("Location: $url");
}
elseif ($num_vote) {
$url=$ref[0]."?pollerr=You+seem+to+have+voted+before.";
header("Location: $url");
}
else {

$sql_vote2="UPDATE poll2 SET votes$vote=votes$vote+1 WHERE pollid=$pollid";
$sql_vote3="INSERT INTO pollstats(pollid, vote, datestamp, voteip) VALUES($pollid, $vote, '$datestamp', '$ip')";

$res_vote2=mysql_query($sql_vote2) or die(mysql_error());
$res_vote3=mysql_query($sql_vote3) or die(mysql_error());

$url=$ref[0];
header("Location: $url");
}
?>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

echo $sql_vote and make sure your vars are both set
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Post by mike08 »

can you explain how to

echo $sql_vote

do i simply just type in echo $sql_vote; if so where would i put it?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Example:

Code: Select all

// <snip>
$sql_vote="SELECT * FROM pollstats WHERE pollid=$pollid AND voteip='$ip'"; 
echo $sql_vote;
exit;
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Post by mike08 »

this is the error i got when i just put that code in you told me too.

what does this mean please?


SELECT * FROM pollstats WHERE pollid= AND voteip='127.0.0.1'
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

mike08 wrote:this is the error i got when i just put that code in you told me too.

what does this mean please?


SELECT * FROM pollstats WHERE pollid= AND voteip='127.0.0.1'
Note the "pollid= AND" ?
"Aha!" you should think, "The $pollid=$_GET["pollid"]; in my code above must be malfunctioning!".

A tip is to check if:

Code: Select all

// this row...
$pollid=$_GET["pollid"];
// perhaps should look like...
$pollid=$_POST["pollid"];
If not, the issue is there somewhere...
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

Try changing this:

Code: Select all

<?php
$sql_vote="SELECT * FROM pollstats WHERE pollid=$pollid AND voteip='$ip'";
?>

to this:

Code: Select all

<?php
$sql_vote="SELECT * FROM pollstats WHERE pollid='$pollid' AND voteip='$ip'";
?>
Some ' s might do the job sometimes :wink:
Post Reply