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?

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

@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('?','?','?')");

@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.

Member Avatar for LastMitch

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.