简体   繁体   English

PHP的用户名/密码登录问题

[英]Username/Password Login Trouble with PHP

Thanks in advance for help. 在此先感谢您的帮助。 Trying to program a basic login with username and password using php. 尝试使用php使用用户名和密码对基本登录进行编程。 Have created basic form with username and password input box with submit button in html and have also set up phpmyadmin database. 用html中的“提交”按钮创建了带有用户名和密码输入框的基本表单,并且还建立了phpmyadmin数据库。

Have already entered in two entries into the table, and have confirmed that the mysql connection in php is working properly and connecting to database. 已经在表中输入了两个条目,并确认php中的mysql连接正常工作并连接到数据库。 But when I try entering in the correct username and password into the form, I am constantly getting "incorrect password" instead of "You're In". 但是,当我尝试在表单中输入正确的用户名和密码时,我会不断收到“错误的密码”而不是“您在”。

Can anyone please tell me where my coding is off? 谁能告诉我我的编码在哪里关闭? (Sorry for the poor formatting. I was using a funny text editor) (对不起,格式很糟糕。我使用的是有趣的文本编辑器)

<?php

$username = $_POST['username']; 
$password = $_POST['password'];

$domainname     = 'mydomainname'; 
$domainpassword = 'mypassword'; 
$hostname       = 'localhost'; 
$database       = 'mydomainname_...phplogin'; 

if($username && $password)  {

    mysql_connect($hostname, $domainname, $domainpassword) or die("Couldn't Connect!");  
    @mysql_select_db($database) or die("Unable to select database");

    $query = mysql_query("SELECT * FROM users WHERE username='$username'");

    $numrows = mysql_num_rows($query);

    if ($numrows!=0)  {      
        while ($row = mysql_fetch_assoc($query))    {      
            $dbusername = $row['username']; 
            $dbpassword = $row['password'];    
        }

        if ($username==$dbusername&&$password==$dbpassword){        
            echo "You're in!";     
        } else {
            echo "Incorrect Password!"; 
        }

    } else {
        die("I'm afraid there isn't a user with that name!");
    }

} else {
    die("Please enter an actual username and a password!");
}

?>

There was a problem with your SQL query. 您的SQL查询有问题。

I have edited your code.. 我已经编辑了您的代码。

<?php

$username = $_POST['username']; 
$password = $_POST['password'];

$domainname     = 'mydomainname'; 
$domainpassword = 'mypassword'; 
$hostname       = 'localhost'; 
$database       = 'mydomainname_...phplogin'; 

if($username && $password)  {

mysql_connect($hostname, $domainname, $domainpassword) or die("Couldn't Connect!");  
@mysql_select_db($database) or die("Unable to select database");

$query = mysql_query("SELECT * FROM users WHERE username='".$username."'");

$numrows = mysql_num_rows($query);

if ($numrows!=0)  {      
    while ($row = mysql_fetch_assoc($query))    {      
        $dbusername = $row['username']; 
        $dbpassword = $row['password'];    
    }

    if ($username==$dbusername&&$password==$dbpassword){        
        echo "You're in!";     
    } else {
        echo "Incorrect Password!"; 
    }

} else {
    die("I'm afraid there isn't a user with that name!");
}

} else {
die("Please enter an actual username and a password!");
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM