PHP查询无法检查数据库的用户名

时间:2016-05-30 04:01:49

标签: php mysql ajax

所以我创建了一个函数来检查用户输入的数据是否已经存在于我的用户数据库中。该功能在测试环境中工作(但在打开页面时运行)。由于某种原因,无论查询结果如何,它都会将用户输入分配给$ username函数。我想我可能需要AJAX,但我不知道如何。

  <?php

  function checkIfEntered($data, $conn)
  {
     $query = "SELECT * FROM users
        WHERE username= '" . $data . "'";

        if ($result = mysqli_query($conn, $query)) {

      /* determine number of rows result set */
      $row_cnt = mysqli_num_rows($result);

      /* close result set */
      mysqli_free_result($result);

    }

    if (($row_cnt) >= 1)
    {
      return TRUE;
    }

    if (($row_cnt) == 0)
    {
      return FALSE;
    }
  }

  error_reporting(E_ALL);
  ini_set('display_errors',1);
  // define variables and set to empty values
  $servername = "localhost"; $username = "root";
  $password = "root";
  $dbname = "MyDatabase";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }
  $email = '';
  $emailErr = '';
  $usernameErr = '';
  $UserPasswordErr = '';
  $username = '';
  $UserPassword = '';
  $confirm = '';
  $confirmErr = '';
  function test_input($data) 
  {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
  }
  if ($_SERVER["REQUEST_METHOD"] == "POST") 
    {
     if (empty($_POST["username"])) 
     {
       $usernameErr = "username is required";
     } 
     else 
     {
      if (checkIfEntered($username, $conn))
     {
      $usernameErr = "There is already a user with the username: " . $username;
     }
     else
     {
       $username = test_input($_POST["username"]);
     }
     }

3 个答案:

答案 0 :(得分:0)

你甚至没有打电话给&#34; checkIfEntered&#34;功能。相反,你正在调用&#34; isEnteredUsername&#34;功能,我在这里看不到它。所以代码总是在else中,并将POST值分配给$ username。 我不确定为什么你认为AJAXing会解决这个问题,但是你可以在文件就绪上使用AJAX调用,或者像这里解释的那样$ .ajax({...})。 http://api.jquery.com/jquery.ajax/

答案 1 :(得分:0)

你忘记了返回记录此功能“checkIfEntered”

的数量
<?php
function checkIfEntered($data, $conn)
{
  $query = "SELECT * FROM users
  WHERE username= '" . $data . "'";

  if ($result = mysqli_query($conn, $query)) {

    /* determine number of rows result set */
    $row_cnt = mysqli_num_rows($result);

    /* close result set */
    mysqli_free_result($result);
    return  $row_cnt;
  }

  if (($row_cnt) >= 1)
  {
    return TRUE;
  }

  if (($row_cnt) == 0)
  {
    return FALSE;
  }
}

error_reporting(E_ALL);
ini_set('display_errors',1);
// define variables and set to empty values
$servername = "localhost"; $username = "root";
$password = "root";
$dbname = "MyDatabase";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$email = '';
$emailErr = '';
$usernameErr = '';
$UserPasswordErr = '';
$username = '';
$UserPassword = '';
$confirm = '';
$confirmErr = '';
function test_input($data) 
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
  if (empty($_POST["username"])) 
  {
    $usernameErr = "username is required";
  } 
  else 
  {
    if (checkIfEntered(test_input($_POST["username"]), $conn))
    {
      $usernameErr = "There is already a user with the username: " . $username;
    }
    else
    {
      $username = test_input($_POST["username"]);
    }
  }
?>

答案 2 :(得分:0)

此代码的问题是函数checkIfEntered()出现错误,因为当您传递 $username 参数时,它尚未分配。所以它会变得虚假。

代码:

  if (checkIfEntered(test_input($_POST["username"]), $conn))
{
  $usernameErr = "There is already a user with the username: " . $username;
}
else
{
  $username = test_input($_POST["username"]);
}

校正

  $username = test_input($_POST["username"]);

  if (checkIfEntered(test_input($_POST["username"]), $conn))
{
  $usernameErr = "There is already a user with the username: " . $username;
  $username = '';
  //set username back to nothing to avoid more errors.
}
else
{
  $username = test_input($_POST["username"]);
}