Jump to content

Need help creating a record with OOP


eldan88

Recommended Posts

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 :happy-04:

 

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.