duanlvxing7707 2018-05-18 12:23
浏览 51
已采纳

数据库找不到用户(html输入字段)

So i'm trying to get the number (or just when the user is found a 1 pops up)of the user im logging into as from the input fields which i have made in phpmyadmin. When i enter something completely random i get 0 as the randomly entered letters arent in my database so it isnt found in the database, however when i enter the actual login and password from one of the users i get 0 instead of 1 which would mean that that user was found from the database. im not sure if i can link the database but here it is: http://localhost/phpmyadmin/sql.php?db=mysqldb1&table=users&token=dbc781132ba546cedab4644522745917&pos=0 here are the codes: here is login.html

<html>
    <head>
        <title> MYSQL </title>
        <script
         src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous">
        </script> 
    </head>
    <body>
    <div id="status"></div>
    <input id="login" placeholder="Login"><br>
<input type="password" id="pass" placeholder="Password"  ><br>
    <button id="entry"> Login </button>
    <script>
    $("#entry").click(function(){
        $.post("check.php", {login: $("#login").val(), password: $("#pass").val()},
        function(result){
        $("#status").html(result);

    })
})
    </script>
    </body>
 </html>

here is the php code

  <?php
    $login=$_POST['login'];
    $password=$_POST['password'];

    echo $login.$password;

    require "config/con1.php";

    $sql= "SELECT id FROM users WHERE login='$login' AND password='$password' ";

    $result=mysqli_query($con, $sql);
    $value = mysqli_num_rows($result);
    echo $value;

    ?>
  • 写回答

1条回答 默认 最新

  • doufei2662 2018-05-18 13:12
    关注

    When you require or include something, the code within that file is run within the same scope as the code where the command is issued. This means that anything that you do within the included file effects the rest of your code within this scope, including assigning values to variables.

    Specific relevant quote from the official docs:

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

    You are currently doing $password=$_POST['password']; near the top of your file. Then you require "config/con1.php";. If you assign a new value to $password within con1.php (which you were), that value will carry over back to your main file (check.php).

    You can solve this either by moving require "config/con1.php"; to the top of check.php or by using different variable names within con1.php (or both).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题