Page 1 of 1

problem creating poll using mysql

Posted: Mon Apr 19, 2004 1:05 pm
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?

Posted: Mon Apr 19, 2004 1:54 pm
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.

code posted

Posted: Mon Apr 19, 2004 2:17 pm
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");
}
?>

Posted: Mon Apr 19, 2004 2:19 pm
by magicrobotmonkey
echo $sql_vote and make sure your vars are both set

Posted: Mon Apr 19, 2004 2:38 pm
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?

Posted: Mon Apr 19, 2004 3:09 pm
by JAM
Example:

Code: Select all

// <snip>
$sql_vote="SELECT * FROM pollstats WHERE pollid=$pollid AND voteip='$ip'"; 
echo $sql_vote;
exit;

Posted: Mon Apr 19, 2004 4:02 pm
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'

Posted: Mon Apr 19, 2004 4:13 pm
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...

Posted: Tue Apr 20, 2004 11:20 am
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: