eldan88 Posted April 16, 2013 Share Posted April 16, 2013 (edited) Hey, I am trying to crearte a record. (I'm just testing it out) But I am not getting any luck doing so. I get the following error message everytime I try. "Database query failed: 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 'INSER INTO USERS ( username, password, first_name, last_name ) VALUE ( 'j' at line 1" Anyhelp is greatly apperciated below is object and method class User { public $id; public $username; public $password; public $first_name; public $last_name; public function create() { global $database; $sql = "INSER INTO USERS ( username, password, first_name, last_name ) VALUE ( '{$this->username}', '{$this->password}', '{$this->first_name}', '{$this->last_name}' )"; if($database->query($sql)) { $this->id = $database->insert_id(); return true; } else { return false; } } } And here is the values I am passing to the attributes. <?php $user = new User(); $user->username = "johnsmith"; $user->password = "abcd1234"; $user->first_name = "John"; $user->last_name = "Smith"; $user->create(); Edited April 16, 2013 by eldan88 Quote Link to comment Share on other sites More sharing options...
Solution Jessica Posted April 16, 2013 Solution Share Posted April 16, 2013 Insert. Tuh. Not inser. Quote Link to comment Share on other sites More sharing options...
trq Posted April 17, 2013 Share Posted April 17, 2013 And please, just because your using classes does not make your code instantly OOP. OOP is a paradigm, a process. Your code breaks much of the paradigms OOP is based on by relying on global state. Quote Link to comment Share on other sites More sharing options...
Hall of Famer Posted April 17, 2013 Share Posted April 17, 2013 (edited) Well OP may want to take a look at this article, the comments are quite worth reading too. http://blog.ircmaxell.com/2012/07/oop-vs-procedural-code.html But regarding your class, yes Jess is right that your SQL statement is wrong. It may be a good idea to use a query object to build queries if you are prone to mysql syntax errors/typos. Edited April 17, 2013 by Hall of Famer Quote Link to comment Share on other sites More sharing options...
eldan88 Posted April 18, 2013 Author Share Posted April 18, 2013 Insert. Tuh. Not inser. Wow, I can't belive I didn't come across that typo. Thank you jess Quote Link to comment Share on other sites More sharing options...
eldan88 Posted April 18, 2013 Author Share Posted April 18, 2013 And please, just because your using classes does not make your code instantly OOP. OOP is a paradigm, a process. Your code breaks much of the paradigms OOP is based on by relying on global state. Ok no problem. Sorry I am just new to learning how to use OOP, so I call everything related to OOP, "OOP". But I will keep what you said mind. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.