dongzhuo0895 2018-05-18 07:58
浏览 119
已采纳

如何处理include语法中的内容

I'm new to web development and I'm trying to make a login using PHP. The content of mylogin.php is getting processed immediately just after loading the file index.php, I'm a bit confused in here. Thie screenshot below is the reponse just after loading the index file.

response just after loading the index.php without doing anything

index.php

  <!DOCTYPE html>
    <html>
    <head>
        <title>
            Welcome     
        </title>
    </head>
    <body>

    <h1> Please login</h1>
    <br>

    <!--
    <form action="" method="POST">
    <label>Username: </label><br>
    <input type="text" name="username" placeholder="Enter username"/><br>
    <label>Password: </label><br>
    <input type="password" name="password" placeholder="Enter password"/><br><br>
    <input type="submit" name="submit" value="Login"/><br><br>
    </form>
    -->

    <form method="post" action="">
    <label>Username:</label><br>
    <input type="text" name="username" placeholder="username" /><br><br>
    <label>Password:</label><br>
    <input type="password" name="password" placeholder="password" />  <br><br>
    <input type="submit" name="submit" value="Login" /> 
    </form>

    </body>
    </html>

    <?php
    include("mylogin.php");
    ?>

mylogin.php

<?php
    include("myconnection.php");

    $error="";
    if(empty($_POST["submit"]))
    {
        if($_POST["username"] =='' || $_POST["password"]=='')
        {
            $error='Please fill the blanks!';

        }else
        {
            $username=$_POST['username'];
            $password=$_POST['password'];

            $sql="SELECT * FROM users WHERE username='$username' AND password='$password'";
            $result=mysqli_query($db,$sql);
            $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

            if(mysqli_num_rows($result)==1)
            {
                $login_user=$_POST["$username"];    
                header("location: myhome.php");

                $error="Connected";

            }
            else
            {

            //$error="Incorrect Username/Password";
            //  echo"failed";

            }

        }
    }
    else
    {

        echo"failed to  submit2";

    }

?>
  • 写回答

2条回答 默认 最新

  • dongyanju1094 2018-05-18 08:07
    关注

    Change the way you include mylogin.php file like given in below code: And there won't be this problem.

    Welcome

    <h1> Please login</h1>
    <br>
    
    <!--
    <form action="" method="POST">
    <label>Username: </label><br>
    <input type="text" name="username" placeholder="Enter username"/><br>
    <label>Password: </label><br>
    <input type="password" name="password" placeholder="Enter password"/><br><br>
    <input type="submit" name="submit" value="Login"/><br><br>
    </form>
    -->
    
    <form method="post" action="">
    <label>Username:</label><br>
    <input type="text" name="username" placeholder="username" /><br><br>
    <label>Password:</label><br>
    <input type="password" name="password" placeholder="password" />  <br><br>
    <input type="submit" name="submit" value="Login" /> 
    </form>
    
    </body>
    </html>
    
    <?php
    if(isset($_POST["submit"]))
    {
        include("mylogin.php");
    }
    ?>
    

    And of course, change your if condition of your mylogin.php file like this:

    if(isset($_POST["submit"])) OR you can change it 
    if(!empty($_POST["submit"]))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题