我无法将数据库表连接到我的网站以进行注册和登录

时间:2019-04-16 18:28:47

标签: php mysql

我的网站可以在localhost上正常运行,但是现在我将其放置在虚拟主机上,该帐户无法使用,我无法注册或登录。我知道它有效,因为它以前曾经这样做过,所以与数据库中的用户表的连接存在问题。带有产品的其他表已成功连接。如何在我的代码中解决此问题?

<?php
include ('connect.php');
$conn = connect();

$email  = $_POST['Email'];
$pass   = $_POST['Password'];
$type   = $_POST['type'];

if($type == "signup"){
    $sql = $conn->prepare("SELECT * FROM user WHERE Email like ?");
    $sql->bind_param('s', $email);

    $sql->execute();
    $result = $sql->get_result();

    if ($result->num_rows > 0) {
        echo "This email is already in use"; 
    }
    else{
        /*Part 1*/
        $sqlInsert = $conn->prepare("INSERT INTO `user` (`UID`, `Email`, `Password`, `FName`, `LName`, `Country`, `City`, `Address`) VALUES (NULL, ?, ?, '', '', '', '', '');");
        $sqlInsert->bind_param('ss', $email, $pass);

        $sqlInsert->execute();

        echo "Success!";
    }
}
else if($type == "login"){
    $sql = $conn->prepare("SELECT * FROM user WHERE Email like ?");
    $sql->bind_param('s', $email);

    $sql->execute();
    $result = $sql->get_result();

    if ($result->num_rows > 0) { 
        while($row = $result->fetch_assoc()){
            if($row["Password"] == $pass){
                // Log in
                session_start();
                $_SESSION['login_user'] = $email;
                $_SESSION['account_id'] = $row["UID"];
                echo "Success!";
            }
            else{
                echo "Wrong credentials!";
            }
        }
    }
    else{
        echo "Wrong credentials!";
    }
}
mysqli_close($conn);
?>

我认为问题可能在这里

<?php
    include 'config.php';

    function connect() {
        $conn = new mysqli(SERVER_NAME, USER, PASSWORD, DB_NAME);

        if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
        return $conn;
    }
?>

1 个答案:

答案 0 :(得分:0)

您可以使用此代码,请填写以下字段
my_user:默认'root'
my_password:默认''
my_db:您的数据库名称

<?php

include 'config.php';

function connect()
{
    $con = mysqli_connect("localhost", "root", "", "my_db");

    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    return $con;
}

?>