Help with PHP OOP PDO

by exma
2 replies
Hi there,

Guys, I really need your help! I'm seriously stuck in my project...
I learn the OOP in PHP, now I build a LOGIN & RESITER system but, when I try insert new user into the DB I get an error:

Code:
Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'group) VALUES ('NewUserName', '68f7ad648943bf04ec61ae61cf8b3e934549eb9de8220cb18' at line 1' in C:\xampp\htdocs\login\register.php:54 Stack trace: #0 {main} thrown in C:\xampp\htdocs\login\register.php on line 54
I have 2 files with 2 classes, the first its User class:

PHP Code:
class Users {
    private 
$db;
    
    public function 
__construct(){
        
$this->db = new Database();
    }
    
    public function 
create($fields = []){
        
$arr array_values($fields);
        
        if (!
$this->db->insertRow("INSERT INTO users (username, password, salt, fullname, joined, group) VALUES (?, ?, ?, ?, ?, ?)"$arr)) {
            throw new 
Exception('There was a problem creating an account!');
        }
    }

and the second its Database class with Insert Method:

PHP Code:
    public function insertRow($query$params){
        try {
            
$stmt $this->datab->prepare($query);
            
$stmt->execute($params);
            return 
TRUE;
        } catch (
Exception $e){
            throw new 
Exception($e->getMessage());
        }
    } 
The $arr variable give me array with user info:

Code:
Array
(
    [0] => NewUserName
    [1] => 0178890e573c0aeb6875def9231230defaf021f2e1747ae40d8d384a9d1eb0d2
    [2] => ùë....OÉesæ":Æ'·\áè!Èû0ISIõòá1
    [3] => UserFullName
    [4] => 2015-07-05 21:15:39
    [5] => 1
)
If I fill manually all information into the query, everything is work! But if I use the variable with array, its isn't work

I tried anything that I know, I have no idea what to do.... I'm really frustrated...
Can anyone help me please? I will very that you 4 all your help!
#oop #pdo #php
  • Profile picture of the author chickahoona
    group is a keyword so for using it as a column name you need to put it into ''
    So something like this:

    Code:
    ... ('username', 'password', 'salt', 'fullname', 'joined', 'group') ...
    {{ DiscussionBoard.errors[10153385].message }}
    • Profile picture of the author exma
      Originally Posted by chickahoona View Post

      group is a keyword so for using it as a column name you need to put it into ''
      So something like this:

      Code:
      ... ('username', 'password', 'salt', 'fullname', 'joined', 'group') ...
      Thank you very very very very much!!! Now it work!
      {{ DiscussionBoard.errors[10154085].message }}

Trending Topics