Jaeger
Jaeger

Reputation: 1754

Unable to insert datas in the database using PDO

I'd like to create a sign up form using PDO, but for an unknown reason, everything works excepted the writing into the database.

I'm quite stuck since Friday, and I'm kinda beginner in PDO, I believe I didn't saw something, but I can't tell what... Any idea ?

    <?php

include('bdd.php'); //Allows to connect to the db from an other file

?>


<form name="inscription" action="confirmation.php" method="POST">
  <label for 'prenom'>Prénom: </label>
  <input type="text" name="prenom" required placeholder="Prénom"/>
  <label for 'nom'>Nom: </label>
  <input type="text" name="nom" required placeholder="Nom"/>
  <label for 'passe'>Mot de passe : </label>
  <input type="password" name="passe" required/>
  <label for 'confirm_passe'>Confirmez le mot de passe : </label>
  <input type="password" name="passe" required/>
  <label for 'email'>Email: </label>
  <input type="email" name="email"/ required placeholder="Adresse e-mail">
  <label for 'telephone'>Téléphone: </label>
  <input type="telephone" name="telephone"/ required placeholder="ex: 0123456789">
  <select name="fonction" id="fonction" required="required">
    <option value="">Selectionnez votre fonction</option>
    <option value="choix1">Présiden(e)</option>       
    <option value="choix2">Vice-Président(e)</option>
    <option value="choix3">Administrateur</option>
    <option value="choix4">Directeur/trice</option>
    <option value="choix5">Travailleur social</option>
    <option value="choix6">Secrétaire</option>
</select>
<input type="submit" value="send">
</form>



<?php


if(!empty($_POST['inscription'])){

if ( $_POST['confirm_passe'] != $_POST['passe'] ){
    echo "Passowords don't match";
}

$req = $db->prepare('INSERT INTO inscrits (prenom, nom, passe, telephone, email, fonction, droits) VALUES(:prenom, :nom, :passe, :telephone, :email, :fonction, 0)');
          $req->execute(array(
          'prenom' => $_POST['prenom'],
          'nom' => $_POST['nom'],
          'passe' => $_POST['passe'],
          'telephone' => $_POST['telephone'],      
          'email' => $_POST['email'],
          'fonction' => $_POST['fonction'],
          'droits' => $_POST['droits'],
          ));

}
?>

As you can see, I'm not english so please don't hesitate to ask any translation if you need to understand something :)

Thanks !

Upvotes: 0

Views: 129

Answers (3)

sshet
sshet

Reputation: 1160

You are missing :droits (inside VALUES) inside prepare() function.

$req = $db->prepare('INSERT INTO inscrits (prenom, nom, passe, telephone, email, fonction, droits) VALUES (:prenom, :nom, :passe, :telephone, :email, :fonction, :droits)');
          $req->execute(array(
          'prenom' => $_POST['prenom'],
          'nom' => $_POST['nom'],
          'passe' => $_POST['passe'],
          'telephone' => $_POST['telephone'],      
          'email' => $_POST['email'],
          'fonction' => $_POST['fonction'],
          'droits' => $_POST['droits'],
          ));
}

Upvotes: 1

Jaeger
Jaeger

Reputation: 1754

Ok so I finally managed to have something that is actually working, here is the code to have a sign up form that needs to be validated by an administrator if someone needs it in the future:

<?php

include('bdd.php'); //File that allows you to get connected to the server


if(!empty($_POST)){

  // Form has been submitted
  if ( $_POST['confirm_passe'] != $_POST['passe'] ) echo "Passwords don't match";

  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  try{
    //
    $req = $db->prepare('INSERT INTO your_table (table1, table2, table3, table4, rights) VALUES(:value1, :value2, :value3, :value4, :rights)');

    $req->execute(array(
      ':value1' => $_POST['table1'],
      ':value2' => $_POST['table2'],
      ':value3' => $_POST['table3'],
      ':value4' => $_POST['table4'],      
      ':rights' => 0
    ));
    header('location:confirmation.php'); //Redirects you in a page that informs you that you are now registered
  }
   catch (PDOException $e) {
      print $e->getMessage ();
  }
}
?>
<!--And here goes the form that you can adapt at will -->
<form name="inscription" action="inscription.php" method="POST">
  <label for="prenom">Prénom: </label>
  <input type="text" name="prenom" required placeholder="Prénom"/>
  <label for 'nom'>Nom: </label>
  <input type="text" name="nom" required placeholder="Nom"/>
  <label for 'passe'>Mot de passe : </label>
  <input type="password" name="passe" required/>
  <label for="confirm_passe">Confirmez le mot de passe : </label>
  <input type="password" name="confirm_passe" required/>
  <label for 'email'>Email: </label>
  <input type="email" name="email"/ required placeholder="Adresse e-mail">
  <label for 'telephone'>Téléphone: </label>
  <input type="telephone" name="telephone"/ required placeholder="ex: 0123456789">
  <select name="fonction" id="fonction" required="required">
    <option value="">Selectionnez votre fonction</option>
    <option value="choix1">Présiden(e)</option>       
    <option value="choix2">Vice-Président(e)</option>
    <option value="choix3">Administrateur</option>
    <option value="choix4">Directeur/trice</option>
    <option value="choix5">Travailleur social</option>
    <option value="choix6">Secrétaire</option>
</select>
<input type="submit" value="send">
</form>

Thank you for your help !

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

The keys from input_parameters must match the ones declared in the SQL.

see doc

try this:

edit for error message:

set:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

in your bdd.php file for pdo error handling.

then

try{
   
  $req = $db->prepare('INSERT INTO inscrits (prenom, nom, passe, telephone, email, fonction, droits) VALUES(:prenom, :nom, :passe, :telephone, :email, :fonction, 0)');

  $req->execute(array(
              ':prenom' => $_POST['prenom'],
              ':nom' => $_POST['nom'],
              ':passe' => $_POST['passe'],
              ':telephone' => $_POST['telephone'],      
              ':email' => $_POST['email'],
              ':fonction' => $_POST['fonction'],
              ':droits' => $_POST['droits']
              ));

}
 catch (PDOException $e) {
    print $e->getMessage ();
    die;
}

   

Upvotes: 1

Related Questions