Jump to content

Archived

This topic is now archived and is closed to further replies.

Dville

unexpected T_STRING on line 51

Recommended Posts

the code is

[code]$aprid = $_GET['appid'];


if (mysql_query("select * from articles left join votes on articles.id = votes.id WHERE username= '$username' AND articleid= '$id'"))
{
echo "You have already voted on this article before";
}

else {
mysql_query(INSERT INTO votes(username, articleid) VALUES('" .$username. "','" .$aprid. "'));
header('Location: ' . $_SERVER['HTTP_REFERER']);
}[/code]


I just don't get what's breaking this script. line 51 is the mysql_query in the else statement

Share this post


Link to post
Share on other sites
You are missing a double quote after mysql_query( and before INSERT INTO

mysql_query("INSERT IN

Share this post


Link to post
Share on other sites
then i get the error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\www\digg\vote.php on line 53

Share this post


Link to post
Share on other sites
You are probably missing one at the end of that same statement then...

mysql_query("INSERT INTO ... " .$aprid. "')");

Share this post


Link to post
Share on other sites
sweet, now that works
now the code looks like

[code=php:0]$aprid = $_GET['appid'];


if (mysql_query("SELECT * FROM votes WHERE username = '$username' AND articleid = '$aprid'"))
{
echo "You have already voted on this article before.";
}

else {
mysql_query("INSERT INTO votes(username, articleid) VALUES('" .$username. "','" .$aprid. "')");
header('Location: ' . $_SERVER['HTTP_REFERER']);
}[/code]

i only have one thing in my votes table, id of 1, username of admin, and articleid of 84

so unless my username and aprid variable is admin and 84 respectively, the if statement would return null, and go to the else statement. correct?

but why when I do this, no mater what the aprid variable is, 84, 4, 70, it always tells me i've already voted on this article, and doesnt go to the else statement
and thoughts?

Share this post


Link to post
Share on other sites
[quote].. but why when I do this, no mater what the aprid variable is, 84, 4, 70, it always tells me i've already voted on this article, and doesnt go to the else statement
and thoughts?[/quote]

Because what you are testing is whether the query is valid (which it is), not whether it returns a result.

This may be more helpful:

[code]
$query = "SELECT * FROM votes WHERE username = '$username' AND articleid = '$aprid'";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
$count = mysql_num_rows($result);
if ($count==1) {
// you have already voted

... now use the logical balance of your loop[/code]

Share this post


Link to post
Share on other sites
so is there any way i can do this?

I was told from here
http://www.phpfreaks.com/forums/index.php/topic,100521.msg396773.html#msg396773

i could, since it's returning 0 if nothing is there

EDIT - k, lemme test that

Share this post


Link to post
Share on other sites
Sorry, I edited my post to include the solution ... just took a 2-minute break :)

Share this post


Link to post
Share on other sites
kick freaking ass, man i've been stuck on this restricting votes to one per article for over a week, with the mysql join command driving me INSAAAAANE

now i can breathe normal again. thanks alot

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.