如何创建使用PHP连接到数据库的HTML登录?

时间:2017-11-14 10:41:09

标签: php html login xampp

我正在尝试使用PHP脚本创建一个可用的HTML登录页面,该脚本将登录数据与数据库进行比较。 我一直试图让这个工作一段时间,但它确实有效。这是我按下登录按钮时得到的错误代码:

Cannot POST /connectivity.php

我用XAMPP创建了一个数据库(名为leftover_youth)。

UserNameID
userName
pass

目前这是整个页面的HTML代码。

<html>
    <head lang="en">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
        <meta content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui" name="viewport">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/app.js"></script>
        <link  rel="stylesheet" href="css/stylesheet.css"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        <title>Project Bootstrap</title>
    </head>
    <body>
        <header>
                <div class="navlogo">
                    <a href="index.html">
                        <h1 class="Logo">Leftover Youth</h1>
                    </a>
                    <a href="index.html">
                        <img class="logoo" src="img/logoo.png" alt="firstimage"> 
                    </a>
                </div>
        </header>
        <div>
        <fieldset style="width:30%">
            <legend>LOG-IN HERE</legend> 
            <form method="POST" action="connectivity.php"> User <br>
                <input type="text" name="user" size="40"><br> Password <br>
                <input type="password" name="pass" size="40"><br> 
                <input id="button" type="submit" name="submit" value="Log-In"> 
            </form> 
        </fieldset> 
        </div> 
    </body>
</html>

PHP:

<?php define('DB_HOST', 'localhost'); 
define('DB_NAME', 'leftover_youth'); 
define('DB_USER','root'); 
define('DB_PASSWORD',''); 

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

$ID = $_POST['user'];
$Password = $_POST['pass'];

function SignIn()
{
session_start(); //starting the session 
if(!empty($_POST['user'])) //checking User data
{ 
        $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
        $row = mysql_fetch_array($query) or die(mysql_error()); 
        if(!empty($row['userName']) AND !empty($row['pass'])) 
        { 
            $_SESSION['userName'] = $row['pass']; 
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 

        } 
        else 
        { 
                echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; 
        } 
} 
} 
if(isset($_POST['submit'])) { SignIn(); 
} 
?>

2 个答案:

答案 0 :(得分:2)

您需要检查 connectivity.php

的目录/路径

还将 $ ID $密码移至帖子检查

if(isset($_POST['submit'])) { SignIn(); 
 $ID = $_POST['user'];
 $Password = $_POST['pass'];
} 

答案 1 :(得分:0)

首先避免使用长期不推荐使用的mysql,而是使用mysqli

您的问题解决方案很少。

MySQLi的方式是这样的:

$connection = mysqli_connect('localhost', 'username', 'password', 'database');

运行数据库查询也很简单,几乎与旧方法相同:

// Old way
 $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
// New way
 $query = mysqli_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());