user2353818
user2353818

Reputation:

Database returning false, cant figure out where i went wrong?

I am sorry to bother you with such a newbie question, and thank you for taking the time to go over it and answer it.

function dbaddusr($username, $email, $password){
        try{
            $conn = new PDO(CONNECTDATA);
            $stmt = $conn->prepare("INSERT INTO 'users' ('username', 'email', 'password') VALUES (:username, :email, :password)");
            $pass = crypt($password);
            $result = $stmt->execute(array("username" => $username, "email" => $email, "password" => $pass));
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
            return false;
        }
    }

Problem is, $result is always false. (I discovered this by some simple var_dump statements inside the try block. I am very new to this and your help on fixing it is highly appreciated.

Upvotes: 0

Views: 61

Answers (4)

Your Common Sense
Your Common Sense

Reputation: 157896

A non-bloated version with all useless and wrong code cleaned.

function dbaddusr($username, $email, $password){
    global $conn;
    $sql  = "INSERT INTO users (username, email, password) VALUES (?,?,?)";
    $stmt = $conn->prepare($sql);
    $pass = crypt($password);
    $stmt->execute(array($username, $email, $pass));
}

You have to connect ONCE per application, and then use that single connection all the way.

Upvotes: -1

randomizer
randomizer

Reputation: 1649

You are passing $pass in your array and your function accepts $password Check your error messages to get specific details and you will find the problem.

Upvotes: 0

Rikesh
Rikesh

Reputation: 26431

Change quotes to backticks for table & column name in your query,

 $stmt = $conn->prepare("INSERT INTO `users` (`username`, `email`, `password`) VALUES      
        (:username, :email, :password)");

Upvotes: 0

xdazz
xdazz

Reputation: 160853

Don't quote the column names, if you want, use the backticks `

INSERT INTO users (username, email, password) VALUES (:username, :email, :password)

Upvotes: 3

Related Questions