我收到此錯誤:警告:第 21 行 C:\xampp\htdocs\BNP\Login.php 中未定義變數 $loginUser 使用者名稱不存在。我嘗試了其他修復,但它返回相同的錯誤訊息。有人可以幫忙嗎?
<?php //variables for connecting to DB $servername = "localhost"; $username = "root"; $password = ""; $dbname = "unityaccess"; //variables submitted by user if(isset($_POST['loginUser'])){ $loginUser = $_POST['loginUser']; } if(isset($_POST['loginPass'])){ $loginPass = $_POST['loginPass']; } // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT password FROM users WHERE username = '" . $loginUser . "'"; $result = $conn->query($sql); if ($result->num_rows > 0){ // output data of each row while($row = $result->fetch_assoc()) { if($row["password"] == $loginPass){ echo "Login Success, welcome."; } else { echo "Wrong Credentials"; } } } else { echo "Username does not exists"; } $conn = null; ?>
謝謝
P粉1186987402024-03-30 20:33:44
您的 $loginUser 未定義,還要檢查您的表單名稱屬性是否有“loginUser”,並使用此更新的程式碼,我在其中定義了 $loginUser ,它將起作用。
connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT password FROM users WHERE username = '" . $loginUser . "'"; $result = $conn->query($sql); if ($result->num_rows > 0){ // output data of each row while($row = $result->fetch_assoc()) { if($row["password"] == $loginPass){ echo "Login Success, welcome."; } else { echo "Wrong Credentials"; } } } else { echo "Username does not exists"; } $conn = null; ?>