Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having the following error in MySql update code in php when I run the script.

Database query failed because 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 '=


This is code causing the error:

PHP
public function update(){
     global $database;
  
     $sql = "UPDATE users SET ";
     $sql .= "FullName='".$database->mysqlPrep($this->fullName)."',";
     $sql .= "RoleID='". $database->mysqlPrep($this->roleID) ."', ";
     $sql .= "UserName='". $database->mysqlPrep($this->userName) ."',";
     $sql .= "Email='". $database->mysqlPrep($this->email) ."', ";
             $sql .="Password='". $database->mysqlPrep($this->password)."' ";
     $sql .= "WHERE ID=". $database->mysqlPrep($this->id);
     $database->query($sql);
     return ($database->affectedRows() == 1) ? true:false;
 }



The sql script in this update function gives the error when I invoke the function in html. One thing i find strange when debugging is if i take the script outside the function it works. So i can't quite wrap my head round what's going on here.

Php version = 5.4.3
MySql version = 5.5.24

I would appreciate any help. Thanks guys
Posted
Updated 16-Aug-14 21:36pm
v2
Comments
Mohibur Rashid 17-Aug-14 23:55pm    
You did not write what Framework you are using.
If the query fails the simplest thing to do is echoing the query; i.e
if($error) echo $sql;
copy the sql from the display and past it in your sql client window and run. You would get better view of the error.

The one possible error would occure if your id is empty; i.e. if $this->id is null
your query would be something like below:
UPDATE SET ... WHERE ID=;

this is definitely an error;
W Balboos, GHB 18-Aug-14 9:32am    
Your field appear to all be in the form FIELDNAME='value' - but the error notes '=.
This implies a single-quote is out-of-place - maybe as a consequence of either missing input or input that contains troublesome values (for example, a single quote in an input string).

I suggest you build your sql string and, instead of simply executing it, look it over.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900