Jump to content

error in your SQL syntax; check the manual


tazeha

Recommended Posts

Hello

 

I write code to save foreach data to the database.

 

foreach data is arrays .

 

For this reason firs convert array to string and then save to the database but display following error on firefox.

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 'change,lowest,topest) VALUES ('[{"Name":"سu06a9ه بهاu' at line 1

my code

$con = @mysql_connect ("localhost","root", "");                mysql_select_db("coin", $con);   if (!$con)                {  die(mysql_error());   }else {foreach($table_rows as $tr) { // foreach row     $row = $tr->childNodes;    if($row->item(0)->tagName != 'tblhead') { // avoid headers        $data[] = array(            'Name' =>trim($row->item(0)->nodeValue),            'LivePrice' => trim($row->item(2)->nodeValue),            'Change'=> trim($row->item(4)->nodeValue),            'Lowest'=> trim($row->item(6)->nodeValue),            'Topest'=> trim($row->item(8)->nodeValue),            ///'Time'=> trim($row->item(10)->nodeValue),        );    }}    $newstring = json_encode($data);    $date=array();mysql_select_db ( "coin", $con );"CREATE TABLE `Dadoo`(id INT NOT NULL AUTO INCREMENT,name VARCHAR(255),liveprice VARCHAR(255),change VARCHAR(255),lowest VARCHAR(255),topest VARCHAR(255),PRIMARY KEY(id)) ENGINE=MyISAM" or die(mysql_error());$debugquery = mysql_query("INSERT INTO `Dadoo`(name,liveprice,change,lowest,topest) VALUES ('$newstring')");                if (!$debugquery)   {   die(mysql_error());  }}mysql_close();

How fix it.

Edited by tazeha
Link to comment
Share on other sites

I find problem.

 

INSERT INTO `Dadoo`(name,liveprice,`change`,lowest,topest) ...

 

change is escape

 

but give this error after fixing top error.

 

Column count doesn't match value count at row 1 (this mean I have multie columns .. )

Link to comment
Share on other sites

You're trying to INSERT against 5 columns in your database, yet you only supply one VALUE, and it's a json_encoded string of an array of associative arrays. As suggested by the error message, I would suggest you look up the syntax for an INSERT statement (i.e. read the manual).

http://www.w3schools.com/sql/sql_insert.asp

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...