0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but I don't want to condone code that has a clear SQL injection vulnerability which is specifically why I didn't give him just the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.