There are several other questions like that, but I didn't understand them correctly, and also don't know how to copy&change the code to work for me.
I got it working, to check, if an user already exists, but if he does, the user should get inserted, and there's the problem I am facing. Because there is a username and a password to insert, I don't know how to bind the ":feldwert" (shown below), to the $username AND $password. The username-check only required a username.
The code:
try {
$db = new PDO("mysql:dbname=todo;host=localhost",
"root",
"");
}catch (PDOException $e) {
echo "Fehler: " . htmlspecialchars($e->getMessage());
exit();
}
if (isset($_POST["username"]) && isset($_POST["password"]))
{
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT username FROM user WHERE username = :feldwert";
$kommando = $db->prepare($sql);
$wert = $username;
$kommando -> bindParam(':feldwert', $wert);
$kommando -> execute();
if($kommando->rowCount() > 0){
echo "exists!";
} else {
echo "non existant";
$sql = "INSERT INTO user VALUES ('$username', '$password');";
$kommando = db->prepare($sql);
$wert = ...
$kommando -> bindParam(':feldwert', $wert);
$kommando -> execute();
}
}