This question already has an answer here:
I'm working on a simple register page to learn php but i have 2 big problems
When I press register i get this error: Error:INSERT INTO users (username, password) VALUES (hello12345, hello12345) Unknown column 'hello12345' in 'field list'
My if statements which checks the username/password length, and the ones which check if password and confirm password match are beeing ignored
My database table looks like this:
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
);
and the php is
include("config.php");
$username = "";
$password = "";
$passwordConfirm = "";
$usernameError = "";
$passwordError ="";
$passwordConfirmError ="";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["username"]))) {
$usernameError = "Bitte geben Sie einen Benutzernamen ein";
}else {
$username = trim($_POST["username"]);
}
}
if(empty(trim($_POST["password"]))) {
$passwordError = "Bitte geben Sie ein Passwort ein";
} elseif (strlen(trim($_POST["password"])) < 6) {
$passwordError = "Password muss mindestens 6 Zeichen lang sein";
} else {
$password = trim($_POST["password"]);
}
if(empty(trim($_POST["passwordConfirm"]))) {
$passwordError = "Bitte bestätigen Sie ihr Passwort.";
} else {
$passwordConfirm = trim($_POST["passwordConfirm"]);
if($password != $passwordConfirm) {
$passwordConfirmError = "Passwörter stimmen nicht überein";
}
}
if(empty($usernameError) && empty($passwordError) && empty($passwordConfirmError)) {
$sqlStatement = "INSERT INTO users (username, password)
VALUES ($username, $password)";
if(mysqli_query($connection, $sqlStatement)) {
echo "Registrierung erfolgreich";
} else {
echo "Error:" .$sqlStatement . "<br>" . mysqli_error($connection);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Registrieren </title>
</head>
<body>
<div>
<h2>Registrieren</h2>
<p>Bitte erstelle eine Account</p>
<form action="register.php" method="post">
<input type="text" name="username" placeholder="Benutzername">
<input type="text" name="password" placeholder="Passwort">
<input type="text" name="passwordConfirm" placeholder="Passwort bestätigen">
<br>
<button type="submit" name="submit" value="submit">Registrieren</button>
<br>
</form>
</div>
</body>
</html>