简体   繁体   English

MySqli查询在执行时返回PHP错误500

[英]MySqli query returning PHP error 500 when executed

I'm writing a simple log-in form using PHP and a MySql DB. 我正在使用PHP和MySql DB编写一个简单的登录表单。 I had this log-in form which was working fine: 我有一个很好的登录表单:

<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">
  &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php';" class="box">  
 </p>
</form>

As you can see, the information was then processed by the login.php file which reads : 如您所见,该信息随后由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);



?>

This use the dbConnect function which is in the file functions.php and this function reads: 这使用在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());
    }
}

I now want to switch my index.php (the one with the login form) from the previous one to the following one, in order to not use any more the file login.php, but use a function to perform the login instead. 现在,我想将index.php(带有登录表单的那一个)从上一个切换到下一个,以便不再使用文件login.php,而是使用一个函数来执行登录。 So the new index.php reads: 因此,新的index.php内容为:

<?php
require_once '../library/functions.php';


$errorMessage = '&nbsp;';

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">&nbsp;</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">&nbsp;</p>
    <p align="center" class="Stile41"></p>
    <p>
      <!-- end #mainContent --> 
    </p>
    </div>
<!-- end #container --></div>
</body>

</html>

In the functions.php file i then created the doLogin() function which is the following one: 然后,在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);

}

And this is where the issue is. 这就是问题所在。 When this code runs, after the username and password are submitted, the page returns a PHP error 500 without many details to work with. 运行此代码后,提交用户名和密码后,页面将返回PHP错误500,但没有很多细节可使用。 I tried to do an echo of the variables $userName and weirdly it doesn't return any value when the variable is set to: 我试图对变量$ userName进行回显,但是奇怪的是,当变量设置为时,它不返回任何值:

$userName = mysqli_real_escape_string($conn,$_POST['txtUserName']);

if I change it to: 如果我将其更改为:

$userName = $_POST['txtUserName'];

instead the echo returns the value. 相反,回声返回值。 Still this change doesn't make the rest of the code work. 尽管如此,此更改仍无法使其余代码正常工作。 Anyone has any clue about what's going wrong? 有人对出什么问题有任何线索吗? I suspect this being an issue with the MySqli query but any additional insights is appreciated to fix this new code. 我怀疑这是MySqli查询的问题,但是可以通过任何其他见解来修复此新代码。

修复了在函数doLogin()中包含全局$ conn的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM