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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

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

Post 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
?>
 
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

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

Post 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']";
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

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

Post 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
 
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

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

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
Post Reply