duanaoreng9355 2017-08-10 11:21
浏览 14
已采纳

为什么我没有在以下代码中获取变量的值?

I have a login form through which a user can login to the website. I am checking whether the username and password match or not. If it matches, the user will be redirected to the Website, but if it doesn't match, an error message will be shown on the upper part of a form.

For this, I am storing the error message in a variable and then printing it to the upper part of the form. But it is not getting the value of the variable. Codes are as follows:

<?php
$link = mysqli_connect("localhost", "root", "", "youtube_project");
 ?>
<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>Login Form</title>
        <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <div class="login">
  <div class="login-triangle"></div>

  <h2 class="login-header">Log in</h2>
  <form class="login-container" name="form1" action="" method="post">
    <p style="color:red;"><?php if(isset($msg)){ echo $msg; } //Printing error msg, but couldn't get it ?></p>
    <p><input type="text" placeholder="Username" name="username" required></p>
    <p><input type="password" placeholder="Password" name="pwd" required></p>
    <p><input type="submit" name="submit1" value="Log in"></p>
  </form>
</div>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<?php
if(isset($_POST['submit1'])){
  $username = mysqli_real_escape_string($link, $_POST['username']);
  $pwd = mysqli_real_escape_string($link, $_POST['pwd']);
  $sql = mysqli_query($link, "SELECT * FROM admin_login WHERE username='$username' && password='$pwd'");
  if(mysqli_num_rows($sql) >= 1){
    ?>
    <script type="text/javascript">
      window.location="demo.php";
    </script>
<?php
  } else {
    $msg = "Invalid Username/Password combination"; //Storing error msg
  }
}
 ?>
  </body>
</html>
  • 写回答

3条回答 默认 最新

  • douchen7555 2017-08-10 11:24
    关注

    You need to move your PHP code at the top so that the variable $msg gets set or unset.

    Below is the formatted updated code:

    <?php
    $link = mysqli_connect("localhost", "root", "", "youtube_project");
    if(isset($_POST['submit1'])){
        $username = mysqli_real_escape_string($link, $_POST['username']);
        $pwd = mysqli_real_escape_string($link, $_POST['pwd']);
        $sql = mysqli_query($link, "SELECT * FROM admin_login WHERE username='$username' AND password='$pwd'");
        if(mysqli_num_rows($sql) >= 1){
    ?>
    <script type="text/javascript">
        window.location="demo.php";
    </script>
    <?php
        } else {
            $msg = "Invalid Username/Password combination"; //Storing error msg
        }
    }
    ?>
    <!DOCTYPE html>
    <html >
        <head>
            <meta charset="UTF-8">
            <title>
                Login Form
            </title>
            <link rel="stylesheet" href="css/style.css">
        </head>
        <body>
            <div class="login">
                <div class="login-triangle">
                </div>
                <h2 class="login-header">
                    Log in
                </h2>
                <form class="login-container" name="form1" action="" method="post">
                    <p style="color:red;">
                        <?php if(isset($msg)){ echo $msg; } //Printing error msg, but couldn't get it ?></p>
                    <p>
                        <input type="text" placeholder="Username" name="username" required>
                    </p>
                    <p>
                        <input type="password" placeholder="Password" name="pwd" required>
                    </p>
                    <p>
                        <input type="submit" name="submit1" value="Log in">
                    </p>
                </form>
            </div>
            <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
        </body>
    </html>
    

    I would recommend you never store plain text passwords! Please use PHP's built-in functions to handle password security.

    You are wide open to SQL Injections too and should really use Prepared Statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型