P粉2001385102023-08-17 10:39:22
而不是使用while
循环,只需一次捕获fetch_object()
,并使用if
测试是否得到真值结果。
您还应该使用预处理语句,我在下面展示了如何做。
您不应该存储明文密码,应该使用password_hash()
和password_verify()
。我没有在下面显示该代码(它将需要对您的注册脚本进行相应更改)。
$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"; }