登录表单PHP未连接到数据库

时间:2018-12-12 17:32:01

标签: javascript php forms session mysqli

我当前正在制作一个登录表单,但是由于某种原因,与数据库的连接无法正常工作。有人知道代码有什么问题吗?我将在下面发布我的意思。我在学习如何使用PHP时将其作为练习。这是我尝试注册时遇到的错误:

注册由于服务器错误而失败。请稍后再试

config.php

  <?php 
    $hostname = 'localhost'; 
    $username = 'root'; 
    $password = ''; 
    $databaseName = '508658'; 
    //Open the database connection - exit with error message otherwise 
    $connection = mysqli_connect($hostname, $username, $password, $databaseName) or exit("Unable to connect to database!");
    ?>

registration.php

<?php
    session_start();
    include ('config.php');
    //phpinfo();
?>
<!DOCTYPE html>
<html>
<head>
<title>Sign Up Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body style="background-color:#bdc3c7">
    <div id="main-wrapper">
    <center><h2>Sign Up Form</h2></center>
        <form action="register.php" method="post">
            <div class="imgcontainer">
                <img src="imgs/avatar.png" alt="Avatar" class="avatar">
            </div>
            <div class="inner_container">
                <label><b>Username</b></label>
                <input type="text" placeholder="Enter Username" name="username" required>
                <label><b>Password</b></label>
                <input type="password" placeholder="Enter Password" name="password" required>
                <label><b>Confirm Password</b></label>
                <input type="password" placeholder="Enter Password" name="cpassword" required>
                <button name="register" class="sign_up_btn" type="submit">Sign Up</button>

                <a href="index.php"><button type="button" class="back_btn"><< Back to Login</button></a>
            </div>
        </form>

        <?php
            if(isset($_POST['register']))
            {
                @$username=$_POST['username'];
                @$password=$_POST['password'];
                @$cpassword=$_POST['cpassword'];

                if($password==$cpassword)
                {
                    $query = "select * from user where username='$username'";
                    //echo $query;
                $query_run = mysqli_query($connection,$query);
                //echo mysql_num_rows($query_run);
                if($query_run)
                    {
                        if(mysqli_num_rows($query_run)>0)
                        {
                            echo '<script type="text/javascript">alert("This Username Already exists.. Please try another username!")</script>';
                        }
                        else
                        {
                            $query = "insert into userinfotbl values('$username','$password')";
                            $query_run = mysqli_query($connection,$query);
                            if($query_run)
                            {
                                echo '<script type="text/javascript">alert("User Registered.. Welcome")</script>';
                                $_SESSION['username'] = $username;
                                $_SESSION['password'] = $password;
                                header( "Location: homepage.php");
                            }
                            else
                            {
                                echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error. Please try later</p>';
                            }
                        }
                    }
                    else
                    {
                        echo '<script type="text/javascript">alert("DB error")</script>';
                    }
                }
                else
                {
                    echo '<script type="text/javascript">alert("Password and Confirm Password do not match")</script>';
                }

            }
            else
            {
            }
        ?>
    </div>
</body>
</html>

0 个答案:

没有答案