eldan88 Posted April 16, 2013 Share Posted April 16, 2013 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(); Link to comment https://forums.phpfreaks.com/topic/277028-need-help-creating-a-record-with-oop/ Share on other sites More sharing options...
Jessica Posted April 16, 2013 Share Posted April 16, 2013 Insert. Tuh. Not inser. Link to comment https://forums.phpfreaks.com/topic/277028-need-help-creating-a-record-with-oop/#findComment-1425218 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. Link to comment https://forums.phpfreaks.com/topic/277028-need-help-creating-a-record-with-oop/#findComment-1425231 Share on other sites More sharing options...
Hall of Famer Posted April 17, 2013 Share Posted April 17, 2013 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. Link to comment https://forums.phpfreaks.com/topic/277028-need-help-creating-a-record-with-oop/#findComment-1425282 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 Link to comment https://forums.phpfreaks.com/topic/277028-need-help-creating-a-record-with-oop/#findComment-1425613 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 Link to comment https://forums.phpfreaks.com/topic/277028-need-help-creating-a-record-with-oop/#findComment-1425629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.