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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题