PHP MYSQL查询没有将数组传入我的变量

xmakbtuz  于 2023-05-16  发布在  Mysql
关注(0)|答案(2)|浏览(132)

下面是我的查询,当我执行var_dump时,它将$results作为NULL:

function connect($loginName) {
$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));

global $db;
$query = "SELECT email, level, password FROM members WHERE email   = '$loginName'";
$result = $db->query($query);
$results = $result->fetch(PDO::FETCH_ASSOC);
return $results;
}

在尝试更改建议后,它在我的欢迎表单中给了我这两个错误:

功能页:

<?php 
require('database_connection.php');
//include('index.php')
//session_start();
if ((isset($_POST['loginName'])) && (isset($_POST['password']))){
  $_SESSION['loginName'] = $_POST['loginName'];
  $_SESSION['password'] = password_hash($_POST['password'], PASSWORD_BCRYPT);

$password = isset($_SESSION['password']) ? $_SESSION['password'] : "";
$loginName = isset($_SESSION['loginName']) ? $_SESSION['loginName'] : "";

// Connecting to the database
 }

function connect($loginName) {

 global $db;
$query = "SELECT email, level, password FROM members WHERE email = '$loginName'";
$result = $db->query($query);
$results = $result->fetch(PDO::FETCH_ASSOC);
return $results;
}

//Login

function login($loginName, $password) {
$results = connect($loginName);
if ((isset($_SESSION['loginName'])) === $results['email'] && (isset    ($_SESSION['password'])) === $results['password']) 
 {

if ( 'a' === $results['level'] )  {
$level = "Administrator";
 } 
elseif ( 'm' === $results['level'] ) {
 $level = "Member";
}
else $level = '?';

if ($level === "Administrator"){
        header('Location: /tires/admin/home.php');
        exit();
    }elseif ($level === "Member"){
        header('Location: /tires/member/home.php');
        exit();
    }                           
include('logoutform.php');}


else {

echo "Sorry. You are not in our database";

}};


//Trying to verify the password

 if ((isset($_POST['loginName'])) && (isset($_POST['password']))){
  $_SESSION['loginName'] = $_POST['loginName'];
  $_SESSION['password'] = password_hash($_POST['password'], PASSWORD_BCRYPT);
}
$password = (isset($_SESSION['password']));
 $loginName = (isset($_SESSION['loginName']));
 $hash = '';

global $db;
$h = $db->prepare("SELECT password FROM members WHERE email = '$loginName'");
$h->execute();
$hashing = $h->fetchAll();
foreach ($hashing as $hash) {
    return $hash['password'];
    if($hashing) // will return true if succefull else it will return false 
        { 
            echo 'Query is working';// code here for true
        };
 };

if (password_verify($password, $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
 };



//Logout
function logout() {
$_SESSION = array();
 session_destroy();
 };

 function get_name($results) {
$name = preg_split("/@/", $results['email']);
$name = ucfirst($name[0]);
 return $name;
                        };


function allowed_get_params($allowed_params=[]) {
 $allowed_array = [];
    foreach($allowed_params as $param) {
        if(isset($_GET[$param])) {
            $allowed_array[$param] = $_GET[$param];
        } else {
            $allowed_array[$param] = NULL;
    }
    }
    return $allowed_array;
 };

 $get_params = allowed_get_params(['loginName', 'password']);

?>

和,这是登录(注销真的)形式:

<form method="post" id="logoutform.php">

 <fieldset>
  <legend>Logout</legend>  

                      <?php
 //include('includes/functions.php');
                       //if ( $_SESSION['loginName'] === $result['email']      && $_SESSION['password'] === $result['password']) {
               echo "Welcome, ";
               echo "$level, ";
               echo get_name($results); 
              // print_r($_POST);
               print '<br />';
               //}
              // else{
            //var_dump($result);
          ///}
                      ?>  <br />   <br />  <br />     
                        <input type="submit" name="action" value="logout" />

 </fieldset></form>

登录表单是这样的:

<form action="home.php" method="post" id="loginform.inc.html">
<fieldset>

  <legend>Login</legend>    <label for="loginName" class="loginName">Username:</label>
      <input id="loginName" name="loginName" type="text" value="<?php echo     (isset($loginName['loginName'])); ?>"/>
    <label for="password" class="loginName">Password:</label>
      <input id="password" name="password" type="password" value="<?php echo (isset($password['password'])); ?>" />
    <input type="submit" name="submit" value="login" />
</fieldset>
 </form>
ddrv8njm

ddrv8njm1#

变更

$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));

$password = isset($_SESSION['password']) ? $_SESSION['password'] : "";
$loginName = isset($_SESSION['loginName']) ? $_SESSION['loginName'] : "";
xiozqbni

xiozqbni2#

$results似乎是空的,因为您的查询没有返回任何内容。我怀疑你的查询没有返回任何东西,因为你正在搜索TRUE$loginName(或者可能是FALSE),因为这行:

$loginName = (isset($_SESSION['loginName']));

isset()返回true或false。我想这不是你想要的。
也不清楚你的global $db是什么。我希望那是伪代码。如果这是您的文字代码,那么您有一个明显的错误,因为您甚至没有尝试连接到数据库。
此外,您应该使用参数化语句传递登录名,以避免obvious security hole

相关问题