【问题标题】:Variable stored from database not echoing on the HTML page从数据库存储的变量未在 HTML 页面上回显
【发布时间】:2018-02-27 10:06:57
【问题描述】:

我从数据库中存储的变量有问题。它涉及变量$username,它通过while循环从其中的mij DB中获取数据。但是,当我尝试在我的 HTML 页面上显示这个 var 时,它变成空白我在这里做错了什么?

<?php 
session_start();
require_once('connect.php');

if(isset($_POST) AND !empty($_POST)){
    $emaillogin = $_POST['emaillogin'];
    $passwordlogin = md5($_POST['passwordlogin']);

    $sqllogin = "SELECT * FROM `login` WHERE Email = '$emaillogin' OR Username = '$emaillogin' AND Password = '$passwordlogin'";
    $resultlogin = mysqli_query($connection, $sqllogin);
    $count = mysqli_num_rows($resultlogin);

    if($count == 1){
        $_SESSION['user'] = $resultlogin;
        while($row = mysqli_fetch_array($resultlogin)){
        $username = $row['Username'];
        }

        $url = "../index.php";
        $messageok = "User login succesfull!";
        echo "<script type='text/javascript'>alert('$messageok');</script>";
        echo '<script>window.location = "'.$url.'";</script>';
    }else{
        $url = "../index.php";
        $messagenok = "User login failed!";
        echo "<script type='text/javascript'>alert('$messagenok');</script>";
        echo '<script>window.location = "'.$url.'";</script>';
    }
}

?>
<div id="myLeftRow" class="leftrow" style="display: inline-block;">
    <div class="leftrow-row">
    <?php if(isset($_SESSION['user'])){

            echo "<button class='button' id='profilebutton'>".$username."</button>";

            echo "<form method='POST' action='includes/logout.php'><button type='submit 'class='button' id='logoutbutton'>Logout</button></form>";
    }
    else{ 

            echo "<button onclick='logintoggle()' class='button' id='loginbutton'>Login</button>";

            echo "<button onclick='registertoggle()' class='button' id='registerbutton'>Register</button>";
    }
?>

【问题讨论】:

  • 如果您的 html 代码在其他页面上,您需要通过查询字符串(如果重定向)或通过会话传递用户名。
  • 它与 PHP 代码在同一个文件页中,并且都包含在索引页中会导致问题吗?
  • 首先,您应该对条件进行分组。 something AND other OR final 会给出误报。整个页面是空白的,还是只是用户名没有值?
  • 使用旧的密码加密方法(例如sha1md5)是糟糕的散列方法 - 您应该使用较新的方法来散列密码。 PHP 有一个内置的password_hash() 函数,它更安全!
  • 您已经在使用支持 prepared statements 和有限变量输入的 API,您应该使用带有占位符的参数化查询(prepared statements)来保护您的数据库免受SQL-injection !开始使用 mysqli::prepare()mysqli_stmt::bind_param()

标签: php html mysql database


【解决方案1】:

我首先建议不要使用

echo '<script>window.location = "'.$url.'";</script>';

只需使用:

header("Location: ../index.php");`

如果你要去那个页面,在 if 语句中。

在您拥有 $username 的回声下,您拥有:

echo "<button class='button' id='profilebutton'>".$username."</button>";

什么时候应该使用:

echo "<button class='button' id='profilebutton'>$username</button>";

由于您已将用户名保存到变量中,因此您不需要前后的点。 仅当您在 $row['username'] 的位置进行操作时,您才需要在前后添加点,因为这样您将显示来自数据库的原始数据。

但否则搜索错误可能在哪里。

尝试制作一个全新的文件,然后重新写入:

<?
session_start();
require_once('connect.php');

$sqlLogin = "SELECT * FROM login WHERE id=#"; // Just change the # to the id of the user.
$resultLogin = mysqli_query($connection, $sqlLogin);
if(mysqli_num_rows($resultLogin) > 0){
  while($row = mysqli_fetch_array($resultLogin)){
    //Setting the username as the username from the database
    $username = $row['username'];
    // Setting the session['user'] to $username.
    $_SESSION['user'] = $username;
  }
  echo $username;
}
?>

如果这不起作用,请检查您从数据库中输入的实体名称是否拼写正确。如果一个字母很大,那么它需要很大。有时它只是一个小错字。

【讨论】:

  • 是的,当我使用 header 时,它会发生冲突,并且在 php 语法之后无法使用空格:/ 真的很奇怪,所以我修复了它,我会尝试一下,没有 .. 看起来很奇怪因为我确实使用这些点来显示另一个 var 并且确实有效..
  • 这完全取决于您要返回的页面是否在同一个文件夹中。如果是,则不要使用 ../。只需使用 index.php。 ..// 指向上一级目录。
  • 这并没有解决它删除点并将整个字符串放在引号之间,如你所说:/
  • 是的,我知道 ../ 语法:p
  • 这很奇怪。我拿了你的代码,自己试了一下。我将 $username 指向 $_SESSION['username'] 并与管理员建立会话。有用。它不应该有任何问题。顺便说一句,您使用的是什么服务器?
【解决方案2】:

我已经设法弄清楚我的语法是正确的,但是在这里使用 window.location 而不是标题

echo '&lt;script&gt;window.location = "'.$url.'";&lt;/script&gt;';

在从一个页面重定向到另一个页面后,它导致我的所有变量都被删除我不知道为什么......非常奇怪

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 2021-06-07
    • 2010-11-30
    • 1970-01-01
    • 2015-12-28
    • 2012-11-05
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多