P粉2001385102023-08-17 10:39:22
Instead of using a while
loop, just capture fetch_object()
once and use if
to test whether you get a true result.
You should also use prepared statements, I show how to do that below.
You should not store clear text passwords and should use password_hash()
and password_verify()
. I don't show that code below (it will require corresponding changes to your registration script).
$username = $_POST["username"]; if ($username != "") { $password = $_POST["password"]; $accountLevel = $_POST["accountLevel"]; $content = $_POST["content"]; $sql = "SELECT password, accountLevel FROM `accounts1` WHERE username=?"; $stmt = $conn->prepare($sql); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); $post = $result->fetch_object(); if ($post) { if ($post->password != $password) { echo "Usuario o Contraseña incorrecta"; }else if ($post->accountLevel != $accountLevel) { echo "Error: No se pudo Publicar. Error de nivel de cuenta."; }else{ $sql = "INSERT INTO `blogposts`(`title`, `contentHTML`, `publishDate`, `commentSection`, `likes`, `dislikes`, `publisher`, `selector`) VALUES ('',?,?,'',0,0,?,'')"; $stmt = $conn->prepare($sql); $stmt->bind_param("sss", $content, $_POST['publishDate'], $username); $stmt->execute(); echo "Enviado"; } } else { echo "Usuario o Contraseña incorrecta"; } }else{ echo "Usuario no indicado"; }