Damilola Adeleke
Damilola Adeleke

Reputation: 33

Inserting data to mysql using pgo

I'm inserting data into my database. The data were inserted but i'm suppose to get OK as my result after data is inserted but i'm getting my else "Registration not complete".

Pls can anyone help?

here is my code

<?php

require 'functions.php';
require 'lib/password.php';

if(isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];



    if(!empty($username) && !empty($password)){
        $hash = password_hash($password, PASSWORD_BCRYPT);
        $query = "INSERT INTO users (id, username, password) VALUES ('','".$username."','".$hash."')";
        if($conn->query($query)===TRUE){
            echo 'Ok';
        } else{
            echo 'Registration not complete';
        }
    }
}

?>

<form action="register.php" method="POST">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    <input type="submit" value="Register">
</form>

Upvotes: 0

Views: 121

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35357

pdo::query() does not return true on success. It returns a PDOStatement object. Check your condition statement and you will see why it is failing:

if($conn->query($query)===TRUE)

Your query is subject to SQL injections. This is a good example of when you should be using pdo::prepare() and PDOStatement::execute() (aka prepared statements). You should not use user input (POST) directly in a query, in this case $username.

Upvotes: 1

Related Questions