0

Please help, is this the correct thing to do? The user is not created in the mysql database.

<?php

session_start();

if( isset($_SESSION['KlantNr']) ){
    header("Location: /");
}

require 'database.php';

$message = '';

if(!empty($_POST['EmailAdres'])
    && !empty($_POST['password'])
    && !empty($_POST['KlantVoornaam'])
    && !empty($_POST['KlantAchternaam'])
    && !empty($_POST['Adres'])
    && !empty($_POST['Postcode'])
    && !empty($_POST['Plaats'])
    && !empty($_POST['Geboortedatum'])
    && !empty($_POST['MobielNr'])):

    // Enter the new user in the database
    $sql = "INSERT INTO Klant ('EmailAdres', 'password', 'KlantVoornaam', 'KlantAchternaam', 'Adres', 'Postcode', 'Plaats', 'MobielNr', 'Geboortedatum') 
                        VALUES (:EmailAdres, :password, :Klantvoornaam, :Klantachternaam, :Adres, :Postcode, :Plaats, :MobielNr, :Geboortedatum)";
    $stmt = $conn->prepare($sql);

    $stmt->bindParam(':EmailAdres', $_POST['EmailAdres']);
    $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
    $stmt->bindParam(':KlantVoornaam', $_POST['KlantVoornaam']);
    $stmt->bindParam(':KlantAchternaam', $_POST['KlantAchternaam']);
    $stmt->bindParam(':Adres', $_POST['Adres']);
    $stmt->bindParam(':Postcode', $_POST['Postcode']);
    $stmt->bindParam(':Plaats', $_POST['Plaats']);
    $stmt->bindParam(':Geboortedatum', $_POST['Geboortedatum']);
    $stmt->bindParam(':MobielNr', $_POST['MobielNr']);

    if( $stmt->execute() ):
        $message = 'Created user';
    else:
        $message = 'Error';
    endif;

endif;

?>

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\register.php on line 38

Wim
  • 11
  • 1
  • 4
  • 1
    You are enclosing your field names in single quote **'** and that generates errors. If you want to enclose them, you have to use backtick **`** instead. – EhsanT Dec 24 '16 at 00:43

1 Answers1

0
  1. Parameters should be case-sensitive.

    You use placholder :Klantvoornaam, :Klantachternaam, but bind to

    $stmt->bindParam(':KlantVoornaam', $_POST['KlantVoornaam']); $stmt->bindParam(':KlantAchternaam', $_POST['KlantAchternaam']);

You should bind to :Klantvoornaam, :Klantachternaam

  1. You either use `` backticks or nothing when you INSERT. Puttung the field names into '' will result into a syntax error.

    $sql = "INSERT INTO Klant (EmailAdres, password, KlantVoornaam, KlantAchternaam, Adres, Postcode, Plaats, MobielNr, Geboortedatum) 
    
user3606329
  • 2,405
  • 1
  • 16
  • 28