I'm new in PHP PDO and MYSQL. I make a PHP signup script, and i want you to see if that is the right way or not, and i hope you to give me some advices to improve my script.
This is the code:
<?php
$dsn = 'mysql:host=localhost;dbname=users';
$user = 'root';
$pass = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
try {
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$usernameInput = $_POST['username'];
$passwordInput = $_POST['password'];
$nameInput = $_POST['name'];
$emailInput = $_POST['email'];
$qUsername = "SELECT * FROM personalinformations WHERE username = '$usernameInput'";
$dataUserName = $conn->query($qUsername);
$countRowUsername = $dataUserName->rowCount();
$qEmail = "SELECT * FROM personalinformations WHERE email = '$emailInput'";
$dataEmail = $conn->query($qEmail);
$countRowEmail = $dataEmail->rowCount();
if($countRowUsername > 0) {
echo 'Compte Exists';
} elseif($countRowEmail > 0) {
echo 'Email Exists';
} else {
$qSingup = "INSERT INTO personalinformations (name, username, password, email) VALUES ('$usernameInput', '$passwordInput',
'$nameInput', '$emailInput')";
$conn->exec($qSingup);
echo 'Compte Created';
}
}
catch (PDOException $error) {
$error->getMessage();
}
} else {
echo 'You Cannot Browse the Page Directly';
}
thank you very much