28, May 2012

updateProperty: Insert date causes SQL error - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  Php User Class
Topic: updateProperty: Insert date causes SQL error
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: updateProperty: Insert date causes SQL error  (Read 1628 times)
Total Zero
*
Posts: 5
34 credits
Members referred : 0


« on: Nov 29, 2009, 06:35:23 pm »

Hi there. I am using the modified class from  Downlord and I am trying to insert a date into the DB. But whatever I write I always get a MySQL error like ?
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-11-29 17:53:49\" WHERE id = 1'.

Other MySQL commands (the first two) work fine. But how do I have to write and formate the date to pass it through and save it?
$update = array(
    'loginCount' => 'loginCount+1',
    'lastLogin' => 'NOW()',
    'lastVisit' => '"2009-11-29 17:53:49"'
    );
$user->updateProperty($update);?>


That is the updateProperty method from the userclass:
public function updateProperty($properties)
  {
    if(is_array($properties) && count($properties) > 0)
    {
      $i=1;
      $query "UPDATE ".$this->dbTable." SET ";

      foreach($properties AS $k => $v)
      {
        /*if($k == $tbFields['pass'])
        {
          switch(strtolower($this->passMethod))
          {
            case 'sha1':
              $v = sha1($v);
              break;
            case 'md5' :
              $v = md5($v);
              break;
            default:
          }
        }*/
        $query .= "".$this->escape($k)." = ".$this->escape($v)."".(($i++ < count($properties)) ? ', ' ' ');
      }

      $query .= "WHERE ".$this->tbFields['userID']." = ".$this->userID."";
      if($this->mysqli->query($query))
        return true;
      else
      {
        $this->error($this->mysqli->error__LINE__);
        return false;
      }
    }
  else
    return false;
  }
?>
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Nov 29, 2009, 06:47:47 pm »

It looks like the function is incomplete. Please expect a new version later (I've already have a draft but needs some stuff to release)

Until then update the table with queries. Bellow is an untested updateProperty() function for the new release, but it is not working with mysqli. Maybe you can play with that:

public function updateProperty($properties) {

    if(is_array($properties) && count($properties) > 0) {

      $i=1;

      $query "UPDATE `".$this->dbTable."` SET ";

      $c count($properties);//a small optimization :)

      foreach($properties AS $k => $v) {

        $v = ($k == $tbFields['pass']) ? $this->encode_password($v) : $v;

        $query .= '`'.$this->escape($k)."` = '".$this->escape($v)."'".(($i++ < $c) ? ', ' ' ');

      }

      $query .= "WHERE `".$this->tbFields['userID']."` = '".$this->userID."'";

      return mysql_query($query$this->dbConn); 

    }

    return false;

  }
?>


In case you find any bugs, please let me know, I haven't even executed this code before Smiley
« Last Edit: Nov 29, 2009, 06:49:45 pm by Nikolas »

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Total Zero
*
Posts: 5
34 credits
Members referred : 0


« Reply #2 on: Nov 29, 2009, 07:22:27 pm »

Thanks for your quick response. The code you posted does help in case I want to insert only strings in the DB but with the backticks at '".$this->escape($v)."'" I can't insert MySQL code anymore (like NOW() for example).

I think the problem is the escape method which escapes the " when I declare a string in my array ('lastVisit' => '"2009-11-29 17:53:49"')
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Nov 30, 2009, 01:08:39 am »

If you want this only for the NOW() function you can use the php date() function instead. I know it is better to use NOW() but it will work (at least until we find a fast way to do this with the class)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Total Zero
*
Posts: 5
34 credits
Members referred : 0


« Reply #4 on: Dec 01, 2009, 08:25:00 pm »

I think I stick with the auto escape function in updateProperty and use for some fields the old and ordinary query function of mysql :-)
Total Zero
*
Posts: 5
34 credits
Members referred : 0


« Reply #5 on: Dec 01, 2009, 10:24:27 pm »

I noticed that the query method is a private function. That results in "Fatal error: Call to private method flexibleAccess::query() from context" when I try to update.
Code:
$user->query("UPDATE `{$user->dbTable}` SET `lastVisit` = `2008-09-09 18:18:00`, `lastLogin` = NOW(), `loginCount` = loginCount+1 WHERE `{$user->tbFields['userID']}` = '{$user->userID}' LIMIT 1", __LINE__);

In example3.php you use $user->query() as well.
Code:
if ($user->query("UPDATE `{$user->dbTable}` SET `{$user->tbFields['active']}` = 1 WHERE `activationHash` = '$hash' LIMIT 1", __LINE__))

Can you help?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #6 on: Dec 02, 2009, 12:20:11 pm »

Yeah, it should be a public function. Just replace "private" with "public" and it will work. This wont affect anything else

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Total Zero
*
Posts: 5
34 credits
Members referred : 0


« Reply #7 on: Dec 02, 2009, 10:03:09 pm »

Ok, that's cool :-) Now it works. I had to change {$user->dbTable} and {$user->tbFields['userID']} 'cause they were private as well. I set them directly into my script instead of setting them public. I am not much familiar with OO but I think it is not a bad idea to make theses vars private :-)

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #8 on: Dec 03, 2009, 12:32:39 pm »

I guess after the first php5 release we will have to check several things regarding the future development

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=9398
Tags : updateproperty update insert date Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  Php User Class
Topic: updateProperty: Insert date causes SQL error
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 11:41:09 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.