Skip to content

Register Form PHP not inserting values into DB, just reloading the page

I really can not find what am I doing wrong in my registration form, unfortunately the page is just reloading instead of inserting values from form to my DB table.

Register.php

<?php 
require_once("./Connection.php");

if(isset($_POST['submit'])){
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $email  = $_POST['email'];
        $phone  = $_POST['phone'];
        $password = $_POST['password'];
        
        $options = array("cost"=>5);
        $hashPassword = password_hash($password,PASSWORD_BCRYPT,$options);
        
        $sql = "insert into agents (firstName, lastName, email, phone, password) value ('".$firstName."', '".$lastName."', '".$email."','".$phone."','".$hashPassword."')";
        $result = mysqli_query($conn, $sql);
        if($result)
        {
            echo "Registration successfully";
        }
    }
?>

Connection.php

<?php 
$conn = mysqli_connect("localhost","root","","KBHestate");
 
if(!$conn){
    die("Connection error: " . mysqli_connect_error()); 
}

Register Form

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
    <input type="text" name="firstName" value="" placeholder="First Name">
    <input type="text" name="lastName" value="" placeholder="Surname">
    <input type="text" name="email" value="" placeholder="Email">
    <input type="text" name="phone" value="" placeholder="Phone">
    <input type="password" name="password" value="" placeholder="Password">
    <button type="submit" name="submit">Submit</button>
</form>

Advertisement

Answer

Please make sure the following line has no problem when it is interpreted by the PHP:

$options = array("cost"=>5);
$hashPassword = password_hash($password,PASSWORD_BCRYPT,$options);

On the other hand, please make sure that the password field is wide enough to store the $hasPassword data

User contributions licensed under: CC BY-SA
5 People found this is helpful