无法将数据从 sql 查询推送到数组进行比较

Having trouble pushing data from a sql query to an array for comparison

因此,我尝试将表单中的用户输入与数据库中的数据、名字、姓氏和电子邮件进行比较。我的问题是将我的结果与用户输入的结果进行比较。我想做的是将查询的结果放入一个数组中,然后将每个数组项与用户的输入进行比较。但是我无法完成我的过程。我究竟做错了什么?

提前谢谢大家。

P.S。我是 php 新手,所以任何建议也将不胜感激

<?php
$servername = "localhost";
$username = "jon";
$password = "test";
$dbname = "test";

$conn = new mysqli($servername, $username, $password, $dbname);

//test connection
if($conn -> connect_error) {
  die("Connection Error: " . $conn -> connect_error);
}

//input from the user
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$email = $_POST['email'];

//query for the database to select the columns
$queryFirst = "SELECT firstname FROM users";
$queryLast = "SELECT lastname FROM users";
$queryEmail = "SELECT email FROM users";

//query results
$resultFirst = $conn -> query($queryFirst);
$resultLast = $conn -> query($queryLast);
$resultEmail = $conn -> query($queryEmail);

$firstResult = array();
$lastResult = array();
$emailResult = array();

array_push($firstResult, $resultFirst);
array_push($lastResult, $resultLast);
array_push($emailResult, $resultEmail);

$firstValid = mysqli_result::fetch_array($firstResult);
$lastValid = mysqli_result::fetch_array($lastResult);
$emailValid = mysqli_result::fetch_array($emailResult);

//comparing query results to user input
foreach($firstResult as $comp) {
  if(strpos($firstname, $comp) !== false) {
    $firstname = true;
  } else {
    return false;
  }
}

foreach($lastResult as $comp) {
 if(strpos($lastname, $comp) !== false) {
    $lastname = true;
  } else {
    return false;
  }
}

foreach($emailResult as $comp) {
  if(strpos($email, $comp) !== false) {
    $email = true;
  } else {
    return false;
  }
}

//redirection if successful or if failure
$success = "../loggedin.php";
$failure = "../fail.php";

if($firstname && $lastname && $email = true) {
  header($success);
  exit();
} else {
  header($failure);
  exit();
}

$conn -> close();
?>

好的,正如安德鲁斯已经告诉过你的第一件事,你可以在一次查询中获得所有信息。但是如果你只想 select 一行,你应该使用 WHERE 子句告诉要查找的内容。

检查这个:

<?php
$servername = "localhost";
$username = "jon";
$password = "test";
$dbname = "test";

$conn = new mysqli($servername, $username, $password, $dbname);
//test connection
if($conn -> connect_error) {
 die("Connection Error: " . $conn -> connect_error);
}

//input from the user . addslashes is for security, so they won't break your query and potentially abuse it.
$firstname = addslashes($_POST['first']);
$lastname = addslashes($_POST['last']);
$email = addslashes($_POST['email']);

//query for the database to select the columns
$query = "SELECT firstname, lastname, email FROM users WHERE firstname = '$firstname' and lastname = '$lastname' and email = '$email'";

//query results
$result = $conn -> query($query);

$numRows = $result->num_rows;

//redirection if successful or if failure
$success = "../loggedin.php";
$failure = "../fail.php";

if($numRows > 0) {
  header($success);
  exit();
} else {
  header($failure);
  exit();
}

$conn -> close();
?>

尚未测试,但想法是在查询中检查匹配项,而不是事后检查。然后,如果有匹配项,它将 return 至少一行(如果您正确定义了 table,则应该不可能有重复项)。

然后根据你的选择。