Wampserver。在phpmyadmin中,我添加了usersdb和userdata表。但是我的代码不起作用
<?php
include_once("sql_connect.php");
session_start();
$_SESSION['currentuser']=$_POST['usernameinput'];
$uname = $_POST['usernameinput'];
$pass = $_POST['passwordinput'];
$sql = "SELECT * FROM 'user_data' WHERE(
username='".$uname."' and password='".$pass."')";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
if($result[0]>0)
{
header ("location: Ghome.php");
}
else
{
header ("Location: loginform_er_incorrectlogpass.php");
}
?>
当我写正确的用户名和密码时,它不起作用。也许我的代码有问题?
<?php
session_start(); # Starts the session
session_unset(); #removes all the variables in the session
session_destroy(); #destroys the session
include ("LoginForm.php");
echo "<p align='center'><font color='red'>Неправильно указан Логин или Пароль.</font></p>";
?>
要解决当前的问题,请删除表名周围的引号,并习惯于使用反勾号。
SELECT * FROM `user_data` ...
不是这个:
SELECT * FROM 'user_data' ...
(从技术上讲,你甚至不需要在这里打勾,但使用它们是一种很好的做法,有助于在以后的工作中发现各种拼写错误。)
一些附加指针:
- Never将密码存储为纯文本;这是非常糟糕的安全做法。使用哈希和盐析。具体来说,请使用bcrypt
- 请不要使用
mysql_*
;mysql_*
函数已过时、不推荐使用且不安全。请改用MySQLi
或PDO
- 您对SQL注入持开放态度
您在sql中错误地在表名周围使用了单引号,因此应该使用反引号。此外,没有检查POSTed变量。不过,理想情况下,为了避免将来的心痛,可以考虑迁移到使用mysqli或PDO。至少尝试对提供的POST数据进行一些基本过滤
<?php
session_start();
include_once("sql_connect.php");
if( isset( $_POST['usernameinput'] ) && isset( $_POST['passwordinput'] ) ){
$uname = mysql_real_escape_string( $_POST['usernameinput'] );
$pass = mysql_real_escape_string( $_POST['passwordinput'] );
$_SESSION['currentuser']=$uname;
$sql = "SELECT * FROM `user_data` WHERE `username`='".$uname."' and `password`='".$pass."';";
$query = mysql_query( $sql );
$result = mysql_fetch_array( $query );
header('location: ' .( $result[0]>0 ) ? 'Ghome.php' : 'loginform_er_incorrectlogpass.php' );
}
?>
替换此
$query = mysql_query($sql)
具有以下
$query = mysql_query($sql) or die(mysql_error());
并查看您得到的错误