如何纠正我的index.php错误?

时间:2016-03-10 08:41:21

标签: php mysqli

这是我尝试的代码,如果我输入我的电子邮件并通过 保存在我的数据库(MySQL)中的地方将显示如果我正确输入它会显示我以该电子邮件登录。所以我想你可以帮助我解释我编码错误的地方,顺便说一句,我是网络编程的新手,所以,如果你教我最容易理解和最简单的方法,我很高兴。

<?php
    $servername = "localhost";
    $username = "root";
    $password = "crazyjaguar";
    $db = "getstarted";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";




    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //  error_reporting(0);
        if ($_POST['email'] && $_POST['pwd']) {

            // escape
                $email = mysqli_real_escape_string($conn, $_POST['email']);
                $password = mysqli_real_escape_string($conn, $_POST['pwd']);
                // sql email and pass
                $sql = "SELECT email, password FROM gs_users";

                // query esql & espl
                $result = mysqli_query($conn, $sql);

                // fetch_array
                $row = mysqli_fetch_array($result,MYSQLI_BOTH);

                    if ($row['email'] != $email) {

                        die ("No $email registered yet!");
                    }
                    if ($row['password'] = $password) {

                        die ("Wrong $pmail!");
                    }
                        echo "You're logged in as $email";

                        mysqli_close($conn);
    }   
    }

    ?>


    <body>
    <h1>Log In</h1>

    <form action='' method='post'>

    <input type='email' name='email' placeholder='Email'>
    <input type='password' name='pwd' placeholder='Password'><br />
    <a href='forgot.php'>Forgost Password?</a><br />
    <input type='submit' name='signup' value='Signup Here!'><br />
    <input type='submit' name='login' value='LogIn'>
    </form>
    </body>

2 个答案:

答案 0 :(得分:0)

mysqli_connect函数实际上可以使用4个参数。最后一个是数据库名称。

语法将是

mysqli_connect(host,username,password,dbname);
  

即使您没有提供默认值,数据库名称也是可选的   数据库将被使用。

但是你再次指定了

$db = "getstarted";

证明你错过了包括数据库名称:)

然后,您可以使用mysqli_select_db()选择要查询的数据库。

最后我要求你使用PDO

答案 1 :(得分:0)

这里我假设您要首先查看电子邮件,如果电子邮件不存在则会消息“电子邮件尚未注册”,如果电子邮件存在,则检查密码输入,如果密码匹配,那么您将被挂起。

请使用以下代码更改您的代码..

<?php
    $servername = "localhost";
    $username = "root";
    $password = "crazyjaguar";
    $db = "getstarted";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $db);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";




    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //  error_reporting(0);
        if ($_POST['email'] && $_POST['pwd']) {

            // escape
                $email = mysqli_real_escape_string($conn, $_POST['email']);
                $password = mysqli_real_escape_string($conn, $_POST['pwd']);
                // sql email and pass
                $sql = "SELECT email, password FROM gs_users where email = $email";

                // query esql & espl
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0)
                {
                    $row = mysqli_fetch_array($result,MYSQLI_BOTH);
                    if ($row['password'] == $password)
                    {
                        echo "You're logged in as $email";
                    }
                    else
                    {
                        die ("Invalid password!");       
                    }
                }
                else
                {
                    die ("No $email registered yet!");
                }
        }   
    }
    mysqli_close($conn);

    ?>


    <body>
    <h1>Log In</h1>

    <form action='' method='post'>

    <input type='email' name='email' placeholder='Email'>
    <input type='password' name='pwd' placeholder='Password'><br />
    <a href='forgot.php'>Forgost Password?</a><br />
    <input type='submit' name='signup' value='Signup Here!'><br />
    <input type='submit' name='login' value='LogIn'>
    </form>
    </body>

希望这会对你有所帮助。