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();