简体   繁体   English

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

[英]How can I create a HTML login that connects to a database with PHP?

I am trying to create a working HTML Login Page with a PHP script that compares the login data with the Database. 我正在尝试使用将登录数据与数据库进行比较的PHP脚本来创建可工作的HTML登录页面。 I have been trying to get this working for some time now but it doesent really work. 我一直在尝试使它工作一段时间,但确实有效。 This is the Error Code I get when I press on the Login Button: 这是当我按下登录按钮时收到的错误代码:

Cannot POST /connectivity.php

I created a Database (called leftover_youth)with XAMPP. 我使用XAMPP创建了一个数据库(称为leftover_youth)。

UserNameID
userName
pass

This at the moment the HTML code for the whole page. 目前,这是整个页面的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:

<?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(); 
} 
?>

You need to check the directory/path of connectivity.php 您需要检查Connectivity.php的目录/路径

also move the $ID and $Password in to the post check 还将$ ID$ Password移到后检查中

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

First of all avoid using mysql which is long back deprecated, instead use mysqli 首先,避免使用早已弃用的mysql ,而是使用mysqli

There are few solutions for your problem. 解决您的问题的方法很少。

The way with MySQLi would be like this: MySQLi的方式如下:

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

To run database queries is also simple and nearly identical with the old way: 运行数据库查询也很简单,并且几乎与旧方法相同:

// 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());

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

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