我正在使用 cloud9 服务器,并将我的数据库连接到该服务器。
我有一些用于简单登录的 PHP 代码,但它不起作用。
与数据库的连接已打开,但始终显示消息
"username and pas is not correct" (getting result==0)
这是我的代码:
<?php
ob_start();
session_start();
if(isset($_POST['button'])){
$username = $_POST['userName'];
$password = $_POST['password'];
}
//MySQL Database Connect
$conn = mysql_connect(getenv("IP"), getenv("C9_USER"), "");
if (!$conn) {
die("Could not connect: " . mysql_error());
}
$db_selected = mysql_select_db('homework5', $conn);
if (!$db_selected) {
die ("Can't use internet_database : " . mysql_error());
}
$username = mysql_real_escape_string($username);
$query = "SELECT *
FROM users
WHERE (userName = '$userName') and (password = '$password') ;";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) // User not found. So, redirect to login_form again.
{ ?>
<SCRIPT language=JavaScript>
alert("The Password or The UserName is not Correct");
</Script>
<?php
header("Location: login1.php?eror=1"); die();
}
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
// store the needed session data for the home page session
session_regenerate_id();
$_SESSION['userName'] = $userData['userName'];
$_SESSION['Level']= $userData['Level'];
//End the current session and store session data.
// header() is used to send a raw HTTP header to open execute home.php
session_write_close();
switch($userData['Level']){
case 1 : header("Location: maneger.php"); die(); break ;
case 2 : header("Location: intredection.php"); die(); break ;
default: header("Location: login1.php?eror=2"); die(); break;
}
最佳答案
这是您选择的查询
$query = "SELECT * FROM users
WHERE (userName = '$userName') and (password = '$password') ;";
上面的选择查询应该是这样的
$query = "SELECT * FROM users
WHERE (userName = '$username') and (password = '$password')";
您将用户名设置为 $username。但是在选择查询中您使用 $userName 和 extra ;还有。
关于php - PHP 和 MySQL 中的登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849549/