如題,我要做帳號密碼註冊的頁面,需檢查兩次帳號密碼是否輸入一致,若不一致出現錯誤提示於輸入格後方,不知道為什麼我按下註冊後上面的HTML都還在但就是不會刷新$errus的值並顯示在頁面上,我問chatpgt也沒有答案求大大幫忙!!感激不盡><
p.s. vardump($errus)有顯示"帳號輸入不一致",但帳號輸入格後方是原本的空白
以下為程式碼
<?php
session_start();
print "hello";
$username='';
$password='';
$username2='';
$password2='';
$errus='';
$errpa='';
$html=<<<EOT
<form action='login0.php' method='post'>
<p>請輸入帳號(username):
<input type='text' name='username' required value=$username>
<p>請再次輸入帳號(username):
<input type='text' name='username2' required value=$username2> $errus
<br><br>
<p>請輸入密碼(password):
<input type='password' name='password' required value=$password>
<p>請再次輸入密碼(password):
<input type='password' name='password2' required value=$password2> $errpa
<input type='submit' name='register' value=註冊>
</form>
EOT;
print $html;
mysqli_report(MYSQLI_REPORT_OFF);
if(isset($_POST['register'])&&$link=@mysqli_connect('localhost','root','','users'))
{
if(!empty($_POST['username'])&&!empty($_POST['username2'])&&!empty($_POST['password'])&&!empty($_POST['password2']))
{
$username=$_POST['username'];
$username2=$_POST['username2'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$correct=1;
if($_POST['username']!==$_POST['username2'])
{
$errus="<p><font color=red>帳號輸入不一致,請重新輸入!";
echo $errus;
//var_dump($errus);
$correct=0;
//echo "n";
}
if($_POST['password']!==$_POST['password2'])
{
$errpa="<p><font color=red>密碼輸入不一致,請重新輸入!";
echo $errpa;
$correct=0;
}
if($correct)
{
echo "a";
$sql="SELECT username FROM customers WHERE username=?";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
//$result = mysqli_stmt_get_result($stmt);
//var_dump($result);
mysqli_stmt_store_result($stmt); //將結果保存在 stmt 對象中
$num_rows = mysqli_stmt_num_rows($stmt); //獲取記錄數
if($num_rows>0)
{ $errus="<font color='red'>帳號已被使用,請重新輸入!";
}
else
{
$sql = "INSERT INTO customers (username, password) VALUES (?, ?)";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "ss", $username, $password);
if (mysqli_stmt_execute($stmt) === TRUE) {
//註冊成功後重定向到登錄頁面
header("Location: login.php?success=1");
//當被重定向到 login1.php 時,URL 中的 ?success=1 會觸發顯示成功訊息。
exit();
}
}
}
}
}else print mysqli_connect_error();
?>