0

I'm running into a problem trying to insert data into mysql table. My connection to the database seems fine, however all that gets returned is my "Connected Successfully" message - not "Sign up Complete!" My table name is 'user' and I've double checked the value names are correct, so I don't know what it is. Here is my process.php code:


if (empty($_POST["firstname"])) {
    die("First name required!");

}
if(empty($_POST["surname"])) {
    die("Surname required!");
}
    if (! filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
        die("Please enter a valid email!");
}

    if (strlen($_POST["password"]) < 8) {
        die("Password must be atleast 8 characters long!");
    }

    if ( ! preg_match("/[0-9]/", $_POST["password"])) {
        die("Password must contain at least one number!");
    }
   
    if ($_POST['password'] !== $_POST['passwordconfirm']) {
        die("Passwords must match!");   
      }

      $password_hash = password_hash($_POST["password"], PASSWORD_DEFAULT);

      $conn = require __DIR__ . "/database.php";

      $sql = "INSERT INTO user (firstname, surname, email, password_hash) VALUES (?, ?, ?, ?)";
        
      $stmt = $conn->stmt_init();

               $stmt->bind_param("sss",
                                    $_POST["firstname"],
                                    $_POST["surname"],
                                    $_POST["email"],
                                    $password_hash);
            
            $stmt->execute();

            echo "Signup complete!";

here is my database connection class:

<?php
$host = "localhost";
$dbname = "login_db";
$username = "root";
$password = "";

$conn = new mysqli(    $host,
                        $username,
                        $password,
                        $dbname);

                        // Check connection
                        if ($conn->connect_error) {
                           die("Connection failed: " . $conn->connect_error);
                        }
                          echo "Connected successfully";
                        ?>
BateauG
  • 1
  • 2
  • Why did you remove the line about getting a 500 error? Is that not what's happening? – Barmar Aug 16 '22 at 23:23
  • @Barmar Because when i've managed to change it now only displays "Connected successfully". Yet still doesn't insert a new row into my table. I've edited the code above accordingly. – BateauG Aug 16 '22 at 23:29
  • 1
    Add mysqli error checking as explained in the linked question. – Barmar Aug 16 '22 at 23:37
  • `$sql` is never being `prepared`/used see the manual, https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php, `$conn->prepare($sql)` – user3783243 Aug 17 '22 at 00:15

0 Answers0