我正在尝试设置一个简单的帐户登录设置和功能;但是,在运行我的“登录”时功能我似乎一直在打击'不成功登录'。
我使用var_dump函数尝试找到问题,并看到var_dump($row['username']); die;
返回NULL。我觉得问题可能来自我的mysql_query
或mysql_fetch_array
。
非常感谢任何帮助。
<?php
$host = 'localhost';
$database = 'database_name';
$username = 'username';
$password = 'password';
// Create connection
$conn = mysql_connect($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
echo 'Connected successfully. ';
mysql_select_db($database) or die('Unable to select database. ');
$signIn = $_POST['signIn'];
function signIn() {
// Start session
session_start();
// Variables
$ID = $_POST['username'];
$Password = $_POST['password'];
if (!empty($ID)) {
$query = mysql_query('SELECT * FROM `signin` WHERE username = "$ID" AND password = "$Password"');
$row = mysql_fetch_array($query, MYSQL_ASSOC);
// Troubleshooting
var_dump($row['username']); die;
if (!empty($row['username']) AND !empty($row['password'])) {
$_SESSION['username'] = $row['password'];
echo "Successful login. ";
} else {
echo "Unsuccessful login. ";
}
}
}
if(isset($signIn)) {
signIn();
}
?>
下面是当前数据库表结构的屏幕截图。
答案 0 :(得分:2)
问题是变量插值:
mysql_query('SELECT * FROM `signin` WHERE username = "$ID" AND password = "$Password"');
单引号不会将$ ID和$ Password扩展到查询中的值,您需要使用双引号:
mysql_query("SELECT * FROM `signin` WHERE username = '$ID' AND password = '$Password'");