0

I have an error when i submit my form : Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 and nothing is sent in the DB. I make a prepared request and check if there is something in the input with 'isset'.

I dont know what to say more so i write to validate my post.

<form action="sql_request.php" method="POST">
    
                    <div class="form-flex">
                        <div class="left-form">
                            <div>
                                <label for="name">Nom du restaurant</label>
                            </div>
    
                            <div>
                                <input type="name" name="name_restaurant" placeholder="Entrez le nom du restaurant" required>
                            </div>
    
                            <div>
                                <label for="mail">Adresse email</label>
                            </div>
    
                            <div>
                                <input type="email" name="email" placeholder="Entrez votre adresse email" required>
                            </div>
    
                            <div>
                                <label for="password">Mot de passe</label>
                            </div>
    
                            <div>
                                <input type="password" name="password" placeholder="Entrez votre mot de passe" required>
                            </div>
                        </div>
    
                        <div>
                            <div>
                                <label for="type">Type de restaurant</label>
                            </div>
    
                            <div>
                                <input type="name" name="type" placeholder="Fast food, gastronomique..." required>
                            </div>
    
                            <div>
                                <label for="address">Adresse postale de l'établissement</label>
                            </div>
    
                            <div>
                                <input type="address" name="address" placeholder="Entrez votre adresse postale" required>
                            </div>
    
                            <div>
                                <label for="confirm-password">Confirmation du mot de passe</label>
                            </div>
    
                            <div>
                                <input type="password" name="password_conf" placeholder="Confirmez votre mot de passe" required>
                            </div>
                        </div>
                    </div>
    
                    <div class="image-upload">
                        <label for="password">Télécharger une photo du restaurant</label>
                    </div>
    
                    <div>
                        <input type="file" name="file" required>
                    </div>
    
                    <input type="submit" value="Inscription">
                </form>

<?php

$pdo = new PDO('mysql:host=localhost;dbname=reservato;charset=utf8', 'root', '', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);

$name = null;
if (isset($_POST['name'])) {
    $name = htmlspecialchars($_POST['name']);
};

$email = null;
if (isset($_POST['email'])) {
  $email = htmlspecialchars($_POST['email']);
};

$password = null;
if (isset($_POST['password'])) {
  $password = htmlspecialchars($_POST['password']);
};

$type = null;
if (isset($_POST['type'])) {
    $type = htmlspecialchars($_POST['type']);
};

$address = null;
if (isset($_POST['address'])) {
  $address = htmlspecialchars($_POST['address']);
};

$password_conf = null;
if (isset($_POST['password_conf'])) {
  $password_conf = htmlspecialchars($_POST['password_conf']);
};

$file = null;
if (isset($_POST['file'])) {
  $file = htmlspecialchars($_POST['file']);
};

$_SESSION["user"] = $name;

$request = $pdo->prepare('INSERT INTO profil(name_user, restaurant_type, email_user, postal_user, password_user, confirm_password_user, url_img_user) VALUES(:name,:type,:email,:address,:password,:password_conf,:file)');

$request->bindValue(':name', $name);
$request->bindValue(':type', $type); 
$request->bindValue(':email', $email);
$request->bindValue(':address', $address);
$request->bindValue(':password', $password);
$request->bindValue(':password_conf', $password_conf);
$request->bindValue(':file', $file);

$request->execute();
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Nevets08
  • 11
  • 2
  • Doesn't the error message have a bit more detail at the end? E.g. usually it would tell you that a specific column cannot be null, or something like that. That information would help you identify more clearly what is going wrong – ADyson Aug 09 '20 at 22:37
  • 1
    You don't have index `name` on your POST, should be `$_POST['name_restaurant']` – catcon Aug 09 '20 at 22:39
  • 1
    I can tell you now though that `$_POST['file']` will never be set. PHP doesn't handle file uploads like this, there is a different mechanism. You need to find a tutorial about file uploads in PHP - there are lots available online already - and study it carefully – ADyson Aug 09 '20 at 22:40
  • can you post the create statment for your table? May guess is you have an id column as primary key there and have forgotten to set it to autoincrement. so when you try to insert id is 0 and for the second entry you will get the error, that this key exists – Thomas Aug 09 '20 at 23:27

0 Answers0