Page 1 of 1

problem with code... Looks right but ways theres an error

Posted: Tue May 12, 2009 7:52 pm
by kedora19
i have a problem with my code. the error is this

Code: Select all

 
Error: 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 '' at line 1
 
but my line 1 is "<?php"
so i don 't know whats wrong
heres the code i'm using

Code: Select all

<?php
                include '../login/dbc.php';
  
                //first open connection to your database
  
                $id = mysql_real_escape_string($_GET['id']); //catch the id number from url, store it and escape slashes from possible attack.
                $takeuser = mysql_query("SELECT * FROM users WHERE id='$id' LIMIT 1"); //Right now script check id number from url against the database
                if (mysql_num_rows($takeuser) < 1) { //checks if we have that id number in our database
                    echo "Error, Please Try Again"; //inform the user that we found 0 result
                    } else { //or if we found some
                while ($row=mysql_fetch_array($takeuser)) { //Taking the result set
                    $sql="UPDATE friends SET activated = '1' WHERE id = '{$row['id']}' AND friend_id={$row['friend_id']}";
                    if (!mysql_query($sql,$link))
                        {
                        die('Error: ' . mysql_error());
                        }
                    } //closing the loop
                    } //closing the "if" statement
 
                    mysql_close($link); //and then we close connection
?>
 

Re: problem with code... Looks right but ways theres an error

Posted: Tue May 12, 2009 10:45 pm
by Griven
Remove the curly braces from the SQL query.

Change this:

Code: Select all

"UPDATE friends SET activated = '1' WHERE id = '{$row['id']}' AND friend_id={$row['friend_id']}";
To this:

Code: Select all

"UPDATE friends SET activated = '1' WHERE id = '$row['id']' AND friend_id=$row['friend_id']";

Re: problem with code... Looks right but ways theres an error

Posted: Sat May 16, 2009 5:17 pm
by kedora19
Griven wrote:Remove the curly braces from the SQL query.

Change this:

Code: Select all

"UPDATE friends SET activated = '1' WHERE id = '{$row['id']}' AND friend_id={$row['friend_id']}";
To this:

Code: Select all

"UPDATE friends SET activated = '1' WHERE id = '$row['id']' AND friend_id=$row['friend_id']";
it still doesn't work... the error is this

Code: Select all

 
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
 

Re: problem with code... Looks right but ways theres an error

Posted: Sat May 16, 2009 5:36 pm
by seppo0010
The first error you got was a MySQL error, so it referred to line 1 of your SQL query... I would guess that $row['friend_id'] is returning NULL or an empty string.
I suggest you to change the line

Code: Select all

die('Error: ' . mysql_error());
to

Code: Select all

die('Error: ' . mysql_error() . '<br />Query was:' . $sql);
and you may see the query as it was executed, an problaly notice the problem easier.

Re: problem with code... Looks right but ways theres an error

Posted: Sat May 16, 2009 6:15 pm
by Benjamin
I really don't see anything wrong that stands out. Perhaps the friend_id is a string or empty?

Try this:

Code: Select all

 
$sql="UPDATE `friends` SET `activated` = '1' WHERE `id` = '{$row['id']}' AND `friend_id` = '{$row['friend_id']}'";
 
If that doesn't fix it, echo out the query and post it here.