从mysql更改为mysqli时登录无法正常工作 [英] login not working when changing from mysql to mysqli
问题描述
下面有一个代码,它通过匹配数据库中的用户名和密码来登录教师,如果正确,则登录,如果不正确,则显示一条消息.
I have a code below where it logs a teacher in by matching it's username and password in the database, if correct, then log in, if incorrect, then display a message.
<?php
session_start();
$username="xxx";
$password="xxx";
$database="mobile_app";
$link = mysqli_connect('localhost',$username,$password);
mysqli_select_db($link, $database) or die( "Unable to select database");
foreach (array('teacherusername','teacherpassword') as $varname) {
$$varname = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" id="teachLoginForm">
<p>Username</p><p><input type="text" name="teacherusername" /></p> <!-- Enter Teacher Username-->
<p>Password</p><p><input type="password" name="teacherpassword" /></p> <!-- Enter Teacher Password-->
<p><input id="loginSubmit" type="submit" value="Login" name="submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT * FROM Teacher t
WHERE
(t.TeacherUsername = '".mysqli_real_escape_string($teacherusername)."')
AND
(t.TeacherPassword = '".mysqli_real_escape_string($teacherpassword)."')
";
$result = mysqli_query($link, $query);
$num = mysqli_num_rows($result);
$loged = false;
while($row = mysqli_fetch_array($result))
{
if ($_POST['teacherusername'] == ($row['TeacherUsername']) && $_POST['teacherpassword'] == ($row['TeacherPassword']))
{
$loged = true;
}
$_SESSION['teacherforename'] = $row['TeacherForename'];
$_SESSION['teachersurname'] = $row['TeacherSurname'];
$_SESSION['teacherusername'] = $row['TeacherUsername'];
}
if ($loged == true){
header( 'Location: menu.php' ) ;
}else{
echo "The Username or Password that you Entered is not Valid. Try Entering it Again.";
}
mysqli_close($link);
}
?>
现在的问题是,即使老师输入了正确的用户名和密码,它仍然无法让老师登录.当上面的代码是旧的mysql()代码时,它可以很好地用作老师.可以在用户名和密码匹配时登录,但是当尝试将代码更改为mysqli时,即使用户名和密码匹配,登录也无法正常进行.我在做什么错了?
Now the problem is that even if the teacher has entered in the correct username and password, it still doesn't let the teacher log in. When the code above was the old mysql() code, it worked fine as teacher was able to login when username and password match, but when trying to change the code into mysqli then it causes login to not work even though username and password match. What am I doing wrong?
推荐答案
我对PHP经验很少,但是当我将代码从mysql转换为mysqli时,我发现某些mysqli命令具有不同的synatx.像mysqli_connect一样,具有第四个参数db_name(不知道是否需要此参数).
也有所不同:mysql_real_escape_string($string)
与mysqli_real_escape_string($link, $string)
I have little experience with PHP, but when I converted my code from mysql to mysqli, I discovered that some mysqli commands have different synatx. Like mysqli_connect has 4th parameter db_name (don't know if this required).
Also is different: mysql_real_escape_string($string)
compared to mysqli_real_escape_string($link, $string)
这篇关于从mysql更改为mysqli时登录无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!