Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP&;SQL登录失败?_Php_Sql Server - Fatal编程技术网

使用PHP&;SQL登录失败?

使用PHP&;SQL登录失败?,php,sql-server,Php,Sql Server,我发现在处理基本登录页面时出现了问题,但是这里没有显示任何登录信息,而且我对PHP&SQL太陌生,所以无法正确修复它。不知道哪里错过了: <?php session_start(); include("require_pro.php"); if($_SERVER["REQUEST_METHOD"]=="POST") { $myusername=addslashes($_POST['username']); $mypassword=md5(addslashes($_PO

我发现在处理基本登录页面时出现了问题,但是这里没有显示任何登录信息,而且我对PHP&SQL太陌生,所以无法正确修复它。不知道哪里错过了:

 <?php

session_start();
include("require_pro.php");

if($_SERVER["REQUEST_METHOD"]=="POST")
{
    $myusername=addslashes($_POST['username']);
    $mypassword=md5(addslashes($_POST['password']));

     $sql=" SELECT * FROM user 
            WHERE username='$myusername' and password='$mypassword'

    ";

    $result=mysql_query($sql);
    $count=mysql_num_rows($result);



    if($count==1)
    {
        echo "Login Successfully";


    } 


}


?>

<!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang='en' xml:lang='en'>
    <head>
    <meta http-equiv="Content-Type" content=”text/html; charset=ISO-8859-1" /> 
    </head>
<html>
<title>Login-In</title>
<body>
<fieldset style="border:2px groove; border-color:blue; padding:15px 30px 15px;margin-right:5px;width:350px;height:150px">
<form action="login1.php" method="post">
<p><b>User Name </b>  <input type="text" name="username" size="20px" maxlength="15"></p>
<p><b>Password  </b> <input type="password" name="password" size="20px" maxlength="15"></p>
<div align="left"> <input type="submit" name="submit" value=login></div>
</form>
</fieldset>
</body>


尝试此操作并查看您收到的输出:

 session_start();
    include("require_pro.php");

    $myusername = addslashes($_POST['username']);
    $password = addslashes($_POST['password']);
    $mypassword = md5($password);


    $query = mysql_query("SELECT * FROM user WHERE username='$myusername'and password ='$mypassword'") or die (mysql_error());
    $count = mysql_num_rows($query);

    if($count == 1) {
        echo "Login Successfully";
    } 

您的连接字符串中存在问题。您的连接字符串应为:

<?php 
$server="localhost";
$username="rgun";
$password="";
$database="rgun";

$dbc=mysql_connect($server,$username,$password) or die("Database connection failed");

if($_SERVER["REQUEST_METHOD"]=="POST")
{
    $myusername=addslashes($_POST['username']);
    $mypassword=addslashes($_POST['password']);

  $sql=" SELECT * FROM user 
            WHERE username='$myusername' and password='$mypassword'";

    $result=mysql_query($sql);
    $count=mysql_num_rows($result);



    if($count == 1)
    {
        echo "Login Successfully";


    } 


}
所以,在这里删除md5或在数据库中插入md5格式的密码,然后脚本将运行。我已经测试了我在这个答案中发布的代码,这些代码执行得非常完美,并给出了期望的结果,所以请尝试一下这段代码


mysql_*已被正式弃用,因此请使用mysqli_*或PDO

您共享的代码和数据库存在两个问题

  • 缺少密码哈希:您已将密码存储为未加密/纯文本
  • 在变量赋值过程中使用MD5:在“mypassword”变量赋值过程中使用了MD5()函数
  • 因此,您可以通过以下方式更新“用户”表并加密密码:update
    USER
    SET
    password
    =MD5(
    password
    ); 或者,从mypassword变量赋值中删除md5()函数。从“$mypassword=md5(addslashes($\u POST['password']);”到“$mypassword=addslashes($\u POST['password']);”

    请注意:

  • 不要以纯文本或非加密方式存储密码
  • “user”、“password”等是MySql中的关键字,尽量不要使用数据库/表模式中的关键字

  • 是否确实正确连接到数据库?你能发布你的代码吗?编辑-很抱歉我刚才看到了它。mysql\u connect($server,$username,$mysql\u user,$mysql\u password)是错误的。在哪里定义$mysql\u user和$mysql\u password?连接字符串应该是:sql\u connect($server,$username,$password)是的,我尝试了你的方法,但仍然不起作用。如果您使用mysqli呢?这将如何改变?如果PDO适用,如何使用面向对象的方法?在这里阅读有关mysqli的内容,以便您了解它。@DerrickRuan我已经更新了我的代码,实现了可以得到结果的代码。哦,是的!!你太棒了,@Raja Usman Mehmood,谢谢你为MD5所做的一切,我想我需要在这方面更加努力,;)对谢谢,如果你的工作完成了,那么选择我的答案是最好的,如果我的答案值得最好的。好提示!谢谢你的建议;))很高兴帮助PHPmen同事:)
    <?php 
    $server="localhost";
    $username="rgun";
    $password="";
    $database="rgun";
    
    $dbc=mysql_connect($server,$username,$password) or die("Database connection failed");
    
    if($_SERVER["REQUEST_METHOD"]=="POST")
    {
        $myusername=addslashes($_POST['username']);
        $mypassword=addslashes($_POST['password']);
    
      $sql=" SELECT * FROM user 
                WHERE username='$myusername' and password='$mypassword'";
    
        $result=mysql_query($sql);
        $count=mysql_num_rows($result);
    
    
    
        if($count == 1)
        {
            echo "Login Successfully";
    
    
        } 
    
    
    }
    
    $mypassword=md5(addslashes($_POST['password']));