1,105,340 Community Members

New User method not inserting into database (PDO Mysql)

Member Avatar
ebutt13
Newbie Poster
20 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

I have these methods in my user class which check whether a username already exists and if not, inserts it into the database:

//This function checks to see if the username entered already exists
    Private function check_user_exists()
    {
    $stmt = $this->_db->prepare('SELECT * FROM client_login WHERE Username =  ?');
        $stmt->execute(array($this->_username));
        if ($stmt->rowCount() > 0) {
        return true;
        }
    }

    public function new_user()
    {
    if ($this->check_user_exists() == False) //Checks to see that no user currently exists
    {
    //This needs to be re-done
    $stmt = $this->_db->prepare("INSERT INTO client_login(Client_ref,Username,Password) VALUES(?,?,?)");

    //bind name and age to statement then execute
    $stmt->bindParam(1,$this->_clientref);
    $stmt->bindParam(2,$this->_username); 
    $stmt->bindParam(3,$this->_password);
    $stmt->execute();
        echo "No user currently exists!";
    } else {
        return false;
    }


    }

The check_user_exists() method works fine but the new_user() method doesn't insert anything into the database or return an error of any kind. Does anybody know what I might be doing wrong?

LastMitch
Deleted Member
 
0
 

@ebutt13

The check_user_exists() method works fine but the new_user() method doesn't insert anything into the database or return an error of any kind. Does anybody know what I might be doing wrong?

Can you post how you connect to the db using PDO.

This seems wrong:

$stmt = $this->_db->prepare("INSERT INTO client_login(Client_ref,Username,Password) VALUES(?,?,?)");

It should look like this:

$stmt = $db->prepare("INSERT INTO client_login(:Client_ref,Username,Password) VALUES('?','?','?')");
Member Avatar
ebutt13
Newbie Poster
20 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@LastMitch

Thanks for your response!
I'm calling the class like this:

<?php
include("user_class.php");
if($_SERVER["REQUEST_METHOD"] == "POST") {

$pdo = new PDO('mysql:host=localhost;dbname=contisec_portal', 'root', '');

$user = new user($pdo, $_POST['username'], $_POST['password']);
if ($user->login()) {
    $user->start_sessions();
    $user->redirect_user();
    } else {
    $user->error_send();
    }

}
?>

Does this help?
As I said, the other SQL queries work, so I don't think it's a problem with the connection.

LastMitch
Deleted Member
 
0
 

As I said, the other SQL queries work, so I don't think it's a problem with the connection.

The reason why I ask because I need to see what is _db? If you establish your $pdo as the connection then it should be

$pdo = new PDO('mysql:host=localhost;dbname=contisec_portal', 'root', '');

So this should appear like this:

Then on line 4:

$stmt = $pdo->prepare('SELECT * FROM client_login WHERE Username =  ?');

Then on line 16:

$stmt = $pdo->prepare("INSERT INTO client_login(Client_ref,Username,Password) VALUES(?,?,?)");

Then on line 22:

$stmt->execute(array($Client_ref,$Username,$Password));

or

you can leave it alone and just add the array in the execute:

Then on line 4:

$stmt = $this->_db->prepare('SELECT * FROM client_login WHERE Username =  ?');

Then on line 16:

$stmt = $this->_db->prepare("INSERT INTO client_login(Client_ref,Username,Password) VALUES(?,?,?)");

Then on line 22:

$stmt->execute(array(_Client_ref,_Username,_Password));
Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

Hi,

just a suggestion. Since you are using bindParam PDO statement, can it be the data_type be responsible for the query failure? I am not sure, but I think we should try testing this possibility.

try doing this... I am not sure this is going to work, but I think it is worth trying..

$stmt->bindParam(1,$this->_clientref, PDO::PARAM_INT);
$stmt->bindParam(2,$this->_username, PDO::PARAM_STR);
$stmt->bindParam(3,$this->_password, PDO::PARAM_STR);

Before testing the above codes, try testing this method like this without the query..

 public function new_user()
{
  var_dump($this->check_user_exists());
}

If you are not getting the boolean response of 1 or true and 0 or false from the check_user_exists() method, try re-writing it to something like this.

 Private function check_user_exists()
{
$stmt = $this->_db->prepare('SELECT * FROM client_login WHERE Username = ?');
$stmt->execute(array($this->_username));
return (($stmt->rowCount() > 0)? true : false );
}
Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

One more question, what is the value of this variable $this->_clientref???? is this for the auto_increment id? column? if so, you will have to remove it. Mysql will do that for you automatically.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: