I'm new to PHP/SQL and tried a tutorial. I created a database with an user who has read and write permissions. Also a table named "employees" with columns: username, password, email, token
This is my code:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Account erstellen</title>
</head>
<body>
<?php
if(isset($_POST["submit"])){
require("mysql.php");
$stmt = $mysql->prepare("SELECT * FROM employees WHERE USERNAME = :user"); //Username überprüfen
$stmt->bindParam(":user", $_POST["username"]);
$stmt->execute();
$count = $stmt->rowCount();
if($count == 0){
//Username ist frei
$stmt = $mysql->prepare("SELECT * FROM employees WHERE EMAIL = :email"); //Username überprüfen
$stmt->bindParam(":email", $_POST["email"]);
$stmt->execute();
$count = $stmt->rowCount();
if($count == 0){
if($_POST["pw"] == $_POST["pw2"]){
//User anlegen
$stmt = $mysql->prepare("INSERT INTO employees (USERNAME, PASSWORD, EMAIL, TOKEN) VALUES (:user, :pw, :email, null)");
$stmt->bindParam(":user", $_POST["username"]);
$hash = password_hash($_POST["pw"], PASSWORD_BCRYPT);
$stmt->bindParam(":pw", $hash);
$stmt->bindParam(":email", $_POST["email"]);
$stmt->execute();
echo "Account created successfully";
} else {
echo "Passwords doesn't match";
}
} else {
echo "E-Mail is already used";
}
} else {
echo "Username not available";
}
}
?>
<h1>Account erstellen</h1>
<form action="register.php" method="post">
<input type="text" name="username" placeholder="Username" required><br>
<input type="text" name="email" placeholder="Email" required><br>
<input type="password" name="pw" placeholder="Passwort" required><br>
<input type="password" name="pw2" placeholder="Passwort wiederholen" required><br>
<button type="submit" name="submit">Erstellen</button>
</form>
<br>
<a href="index.php">Do you have already a account?</a>
</body>
</html>
After entering username, mail and 2x password into my form and click on "create" I get the message "Account created successfully" but the values aren't in my database.
I tried to debug it with F12(Firefox)the parameters get passed.......
$stmt->execute();use php.net/manual/en/pdostatement.rowcount.php that will tell you if it inserted a row or not. Theexecutecall just means to was sent.$stmt->execute()should not be ignored.