1
<?php
    session_start();
        $con=mysqli_connect("localhost","xxx","xxxxxx","xxx");
            if (mysqli_connect_errno($con))
            {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }


        $eadd = $_POST['eadd'];
        $pass = $_POST['pass'];
        $eadd = htmlspecialchars(stripslashes(strip_tags($eadd)));
        $pass = htmlspecialchars(stripslashes(strip_tags($pass)));
        if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
            $sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
            if(!$sql){
                die('There was an error in query '. mysql_error());
                }
            $count = mysql_numrows($sql) or die(mysql_error());
            if ($count<=0)
                {

                    echo "
                        <html>
                            <style>
                                body{
                                    background-color:#cccccc;
                                    }
                                #error{
                                    position:relative;
                                    margin:auto;
                                    top:20px;
                                    width:320px;
                                    height:55px;
                                    background-color:#63a8d7;
                                    border:1px  solid #2a262a;
                                    }

                                #errorC{
                                    position:absolute;
                                    top:20px;
                                    left:20px;
                                    font: 14px arial, tahoma;
                                    }
                            </style>
                            <body>
                                <div id=error>
                                    <div id=errorC>
                                        Incorrect Email Address and Password! <a href=index.php>GO BACK</a>
                                    </div>
                                </div>
                            </body>
                        </html>
                            ";
                }
                else
                {
                    //have them logged in
                    $_SESSION['account'] = $eadd;
                    header('location:home.php');
                }
                mysqli_close($con);
        } else {
            echo "
                <html>
                    <style>
                        body{
                            background-color:#cccccc;
                            }
                        #error{
                            position:relative;
                            margin:auto;
                            top:20px;
                            width:320px;
                            height:55px;
                            background-color:#63a8d7;
                            border:1px  solid #2a262a;
                            }

                        #errorC{
                            position:absolute;
                            top:20px;
                            left:20px;
                            font: 14px arial, tahoma;
                            }
                    </style>
                    <body>
                        <div id=error>
                            <div id=errorC>
                                Invalid Email Address! <a href=index.php>GO BACK</a>
                            </div>
                        </div>
                    </body>
                </html>
                    ";
        }
?>

为什么会出现“未选择数据库”错误?我的 mysqli_connect 是正确的。我有另一个注册 php 代码,使用它我可以使用该连接注册一些电子邮件地址。但是在登录 php 代码中,我无法使用用户电子邮件地址登录。

4

2 回答 2

2

从上面的代码看起来你正在使用mysqli_connect数据库连接和mysql_query查询执行。使用mysqli_query而不是mysql_query. 像这样

mysqli_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
于 2013-05-19T01:54:03.593 回答
1

实际上,您需要在身份验证后选择一个数据库。在建立数据库连接时,我们需要遵循这些步骤。

  1. 创建连接
  2. 选择数据库
  3. 触发查询
  4. 使用查询结果
  5. 关闭连接

示例代码在这里

首先,您需要在登录后在代码中添加此行

$db_select = mysql_select_db("myDatabase", $con);

其次,您需要$con在选择数据库后作为第二个参数传递所以您的查询语句变成这样

$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'", $con);
于 2013-05-19T01:53:43.863 回答