So i tried to code a Registration system using HTML and php. But when i press the regist button, the page reloads(still same file) and i just get a blank page. I also have all the time a empty red field(There should stand the errors in if somebody submits unvalid information).
I did a little trubleshooting and when i set a echo in line 14 from the authKontroller and set the rest to comment im getting that echo, so the problem is in the authKontroller.
But i cant find the mistake. I tribblechecked all linked stuff and they are linked correctly so i just gonna post the two important codes.
…/Kontroller/authKontroller.php(The file that creates the Mistake)
<?php
session_start();
require "../config/db.php" ;
$errors = array();
$name = "";
$email = "";
//klickt jemand auf registrieren
if(isset($_POST["registrierungs-knopf"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$passwort = $_POST["passwort"];
$passwortbestätigung = $_POST["passwortbestätigung"];
if (empty($name)) {
$errors["name"] = "Ein Name ist erforderlich";
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors["email"] = "Die E-Mail Adresse muss gültig sein";
}
if (empty($email)) {
$errors["email"] = "Eine E-Mail ist erforderlich";
}
if (empty($passwort)) {
$errors["passwort"] = "Ein Passwort ist erforderlich";
}
if($passwort != $passwortbestätigung ) {
$errors["passwort"] = "Die Passwörter müssen übereinstimmen";
}
$emailQuery = "SELECT * FROM users WHERE email=? LIMIT 1 ";
$stmt = $conn->prepare($emailQuery);
$stmt ->bind_param("s", $email);
$stmt ->execute();
$result = $stmt->get_result();
$userCount = $result->num_rows;
$stmt ->close();
if($userCount > 0) {
$errors["email"] = "Email already exists";
}
if (count($errors) === 0) {
$passwort = password_hash($passwort, PASSWORD_DEFAULT);
$zeichen = bin2hex(random_bytes(50));
$bestätigt = false;
$sql = "INSERT INTO users (name, email, bestätigt, zeichen, passwort) VALUES (?, ?, ?, ?, ?,)";
$stmt = $conn->prepare($sql);
$stmt ->bind_param("ssbss", $name, $email, $bestätigt, $zeichen, $passwort );
if ($stmt ->execute()) {
$user_id = $conn->insert_id;
$_SESSION["id"] = $user_id;
$_SESSION["name"] = $name;
$_SESSION["email"] = $email;
$_SESSION["bestätigt"] = $bestätigt;
//flash message
$_SESSION["message"] = "Du bist eigeloggt";
$_SESSION["alert-class"] = "alert-success";
header("location: ../Registrierung/taxinico.php ");
exit();
} else{
$errors["db_error"] = "Database Fehler: Registrierung fehlgeschlagen";
}
}
}
?>
and the registration field …/Registrierung/registriereung.php
<?php require_once "../Kontroller/authKontroller.php" ?>
<!Doctype html>
<html lang="de">
<head><?
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<title>Registrierung</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4 form-div">
<form action="registrierung.php" method="post">
<h3 class="text-center">Registrieren</h3>
<?php if(count($errors > 0)): ?>
<div class="alert alert-danger">
<?php foreach($errors as $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" value="<?php echo $name; ?>" class="form-control form-control-lg Box">
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" name="email" value="<?php echo $email; ?>" class="form-control form-control-lg Box">
</div>
<div class="form-group">
<label for="passwort">Passwort</label>
<input type="text" name="passwort" class="form-control form-control-lg Box">
</div>
<div class="form-group">
<label for="passwortbestätigung">Passwort wiederholen</label>
<input type="text" name="passwortbestätigung" class="form-control form-control-lg Box">
</div>
<div class="form-group button">
<button type="submit" name="registrierungs-knopf" class="w-100 btn btn-lg btn-primary">Registrieren</button>
</div>
<p class="text-center">Schon Registriert? Einfach <a href="einloggen.php">hier</a> Einloggen</p>
</form>
</div>
</div>
</div>
</body>
</html>
Thanks a lot for helping
Paul