5 Replies - 802 Views - Last Post: 26 January 2009 - 11:42 AM Rate Topic: -----

#1 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 900
  • Joined: 08-August 08

sql syntax error.

Posted 26 January 2009 - 10:14 AM

ive just been learning oop programming, from ctphpnwb, and ive mad this function to add a time to the db as i will be needing a lot of timestamps saved in my db

this is the function
	function update_time($fieldName, $wait){
		$waittime = time() + $wait;
		$next = date("Y-m-d H:i:s", $waittime);
		$query = "UPDATE ".$this->table[0]." SET '".$fieldname."' = '". $next ."' WHERE id= '".$this->id."' ";
		mysql_query($query,$this->dblink) or die(mysql_error());
	}


and this is where it is passed

	$this->update_time('next_crime', $this->wait[$x]);


i dont see anything wrong with the previous syntax nor with the following error, can someone help please

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 ''' = '2009-01-26 08:54:13' WHERE id= '1'' at line 1


please help me on how to fix this error

Is This A Good Question/Topic? 0
  • +

Replies To: sql syntax error.

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,003
  • Joined: 08-August 08

Re: sql syntax error.

Posted 26 January 2009 - 10:52 AM

PHP is case sensitive:

$fieldName != $fieldname
Was This Post Helpful? 0
  • +
  • -

#3 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 900
  • Joined: 08-August 08

Re: sql syntax error.

Posted 26 January 2009 - 11:28 AM

arr thanks for that, i misssed it :)

**** fixed that ****

but i still get a similiar 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 ''next_crime' = '2009-01-26 10:17:56' WHERE id= '1'' at line 1

same place

This post has been edited by ghqwerty: 26 January 2009 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: sql syntax error.

Posted 26 January 2009 - 11:37 AM

Wouldn't there be an error on this line

$query = "UPDATE ".$this->table[0]." SET '".$fieldname."' = '". $next ."' WHERE id= '".$this->id."' ";




Shouldn't it be like this

$query = "UPDATE ".$this->table[0]." SET ".$fieldName. " = '". $next ."' WHERE id= ".$this->id;


You had single quotes around the column name that is to be updated. And since id is numeric it doesn't need single quoted does it?
Was This Post Helpful? 0
  • +
  • -

#5 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 900
  • Joined: 08-August 08

Re: sql syntax error.

Posted 26 January 2009 - 11:37 AM

sorry psycho didnt see your post, ill try it


thanks, it works now

This post has been edited by ghqwerty: 26 January 2009 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: sql syntax error.

Posted 26 January 2009 - 11:42 AM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1