Jump to content

Need help creating a record with OOP


eldan88
Go to solution Solved by Jessica,

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();

Edited by eldan88
Link to comment
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.

Edited by Hall of Famer
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.