[英]MySqli query returning PHP error 500 when executed
我正在使用PHP和MySql DB编写一个简单的登录表单。 我有一个很好的登录表单:
<form action="login.php" method="post" enctype="multipart/form-data" name="frmAddEntry" id="frmAddEntry">
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
<tr><td colspan="2" id="entryTableHeader">Login to C-Cur</td></tr>
<tr>
<td width="150" class="label">User ID</td>
<td class="content"> <input name="txtUID" type="text" class="box" id="txtUID" size="50" maxlength="100"></td>
</tr>
<tr>
<td width="150" class="label">Password</td>
<td class="content"> <input name="txtPwd" type="password" class="box" id="txtPwd" size="50" maxlength="100"></td>
</tr>
</table>
<p align="center">
<input name="btnAddEntry" type="submit" id="btnAddEntry" value="Login" class="box">
<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php';" class="box">
</p>
</form>
如您所见,该信息随后由login.php文件处理,其内容为:
<?php
include('../library/functions.php');
dbConnect();
$pass1 = mysqli_real_escape_string($conn,$_POST['txtPwd']);
$userid = mysqli_real_escape_string($conn,$_POST['txtUID']);
$sql = "SELECT *
FROM ccureaccounts
WHERE userid = '$userid'";
if ($result = $conn->query($sql)) {
while($row = $result->fetch_assoc()){
if (password_verify($pass1, $row['password'])) {
echo "User ID: ".$row['userid'] . '<br />';
echo "Balance: ".$row['balance'] . ' $ <br />';
}else { echo "Wrong user ID or Password";}
}
$result->close();
}
dbConnect(false);
?>
这使用在functions.php文件中的dbConnect函数,该函数的内容为:
function dbConnect($close=true){
global $conn;
if (!$close) {
mysqli_close($conn);
return true;
}
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
}
现在,我想将index.php(带有登录表单的那一个)从上一个切换到下一个,以便不再使用文件login.php,而是使用一个函数来执行登录。 因此,新的index.php内容为:
<?php
require_once '../library/functions.php';
$errorMessage = ' ';
if (isset($_POST['txtUserName'])) {
$result = doLogin();
if ($result != '') {
$errorMessage = $result;
}
}
?>
<html>
<head>
<title>C-Cure Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body class="schema">
<div id="container">
<div id="header"><!-- end #header --></div>
<div class="arrotcen">
<h1 align="center" class="Stile1">C-CURE LOGIN</h1>
<div class="errorMessage" align="center"><?php echo $errorMessage; ?></div>
<p align="center"> </p>
<form method="post" name="frmLogin" id="frmLogin">
<table width="271" height="77" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td colspan="2" bgcolor="#000000"><div align="center" class="Stile1"></div></td>
</tr>
<tr>
<td >User ID</td>
<td ><label>
<input type="text" name="txtUserName" id="txtUserName" />
</label></td>
</tr>
<tr>
<td> Password</td>
<td><label>
<input type="password" name="txtPassword" id="txtPassword" />
</label></td>
</tr>
<tr>
<td colspan="2" bgcolor="#333333"><label> </label>
<div align="center">
<input type="submit" name="button" id="button" value="Login" />
</div></td>
</tr>
</table>
<input name="action" type="hidden" id="action" value="login" />
</form>
<p align="center" class="Stile1"> </p>
<p align="center" class="Stile41"></p>
<p>
<!-- end #mainContent -->
</p>
</div>
<!-- end #container --></div>
</body>
</html>
然后,在functions.php文件中,创建了doLogin()函数,该函数如下:
function doLogin()
{
// if we found an error save the error message in this variable
$errorMessage = '';
dbConnect();
$password = mysqli_real_escape_string($conn,$_POST['txtPassword']);
$userName = mysqli_real_escape_string($conn,$_POST['txtUserName']);
$sql = "SELECT *
FROM ccureaccounts
WHERE userid = '$userName'";
if ($result = $conn->query($sql)) {
$errorMessage = '4';
while($row = $result->fetch_assoc()){
if (password_verify($password, $row['password'])) {
header('Location: '.DOMAIN);
exit;
//echo "User ID: ".$row['userid'] . '<br />';
//echo "Balance: ".$row['balance'] . ' $ <br />';
}else { $errorMessage = "Wrong user ID or Password";}
}
$result->close();
}
return $errorMessage;
dbConnect(false);
}
这就是问题所在。 运行此代码后,提交用户名和密码后,页面将返回PHP错误500,但没有很多细节可使用。 我试图对变量$ userName进行回显,但是奇怪的是,当变量设置为时,它不返回任何值:
$userName = mysqli_real_escape_string($conn,$_POST['txtUserName']);
如果我将其更改为:
$userName = $_POST['txtUserName'];
相反,回声返回值。 尽管如此,此更改仍无法使其余代码正常工作。 有人对出什么问题有任何线索吗? 我怀疑这是MySqli查询的问题,但是可以通过任何其他见解来修复此新代码。
修复了在函数doLogin()中包含全局$ conn的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.