无法将用户插入我的数据库

时间:2017-02-04 05:08:23

标签: php html

我希望你们中的一位专家可以在我的PHP中指出我的数据库连接代码中的错误。我的页面根本不起作用。

我正在尝试在我的数据库中创建新用户。我检查了表格中的所有名字,它们完美匹配。

这是我的功能:

<?php include('connection.php'); ?>
    <?php 

    
    function createUser()
    {
        if(isset($_POST['submit']))
        {
            global $connection;
            
            $username = $_POST['username'];
            $password = $_POST['password'];
            $firstName = $_POST['firstName'];
            $lastName = $_POST['lastName'];
            $emailAddress = $_POST['emailAddress'];
            $phoneNumber = $_POST['phoneNumber'];
            $privilege = $_POST['privilege'];
            
            $query = "INSERT INTO users(username,password,firstName,lastName,emailAddress,phoneNumber,privilege)";
            $query .= "VALUES ('$username', '$password','$firstName','$lastName','$emailAddress','$phoneNumber','$privilege')";
            
            $result = mysqli_query($connection, $query);
            
            
            if(!$result) 
            {
                die('Query Failed' . mysqli_error());
            }
            else
            {
                echo '<div class="modal-footer">
                                <div class="alert alert-success alert-block fade in" id="successAlert">
                                <button class="close" type="button" data-dismiss="alert" aria-label="close"><span aria-hidden="true">&times;</span></button>
                                <h4>Success!</h4>
                                <p>New user registered successfully</p>
                            </div>
                        </div>';
                die;
            }
 
        }
    }
    
?>

这是我的HTML: 我将header.php调用到我的主页,但函数也在那里调用。

<?php include('connection.php'); ?>
<?php include('functions.php'); ?>
<?php createUser(); ?>

<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

    <div class="navbar navbar-fixed-top navbar-inverse">
        <div class="container">

            <button class="navbar-toggle" data-target=".navbar-responsive-collapse" data-toggle="collapse" type="button">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <!-- end responsive-dropdown -->

            <a href="index.php" class="navbar-brand">RichmondMC</a>

            <div class="nav-collapse collapse navbar-responsive-collapse">
                <ul class="nav navbar-nav">
                    <li><a href="movieDatabase.php">Movie Database</a></li>
                    <li><a href="gallery.php">Gallery</a></li>
                    <li><a href="ratings.php">Ratings</a></li>
                </ul>
                <!-- end navbar-nav -->
                <ul class="nav navbar-nav pull-right">
                    <li class="dropdown">
                        <a href="" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span> My Account <strong class="caret"></strong></a>

                        <ul class="dropdown-menu">
                            <li><a href="#"><span class="glyphicon glyphicon-wrench"></span> Settings</a></li>
                            <li><a href="#registerModal" role="button" data-toggle="modal"><span class="glyphicon glyphicon-list-alt"></span> Register New User</a></li>
                            <li class="divider"></li>
                            <li><a href="login.php"><span class="glyphicon glyphicon-play"></span> Sign In</a></li>
                            <li><a href="#"><span class="glyphicon glyphicon-off"></span> Sign Out</a></li>
                        </ul>
                        <!-- end dropdown-menu -->
                    </li>
                    <!-- end dropdown -->
                </ul>
                <!-- end myAccount-dropdown -->
            </div>
            <!-- end nav-collapse -->
            
        </div>
        <!-- end container -->
    </div>
    <!-- end navbar -->


    <div class="modal fade" id="registerModal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <a href="#" class="close" data-dismiss="modal">&times;</a>

                            <h4 class="modal-title"><span class="glyphicon glyphicon-th-list"></span> New User Registration </h4>
                        </div>
                        <!-- end modal-header -->
                        <div class="modal-body">
                            <h5>Enter the New Members details including username/password to complete registration</h5><br>

                            <form action="" class="form-horizontal" method="post">
                                <div class="form-group">
                                    <label for="username" class="col-lg-4 control-label">Username:</label>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="password" class="col-lg-4 control-label">Password:</label>
                                    <div class="col-lg-8">
                                        <input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="firstName" class="col-lg-4 control-label">First Name:</label>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control" id="firstName" name="firstName" placeholder="First Name" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="lastName" class="col-lg-4 control-label">Last Name:</label>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control" id="lastName" name="lastName" placeholder="Last Name" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="emailAddress" class="col-lg-4 control-label">Email Address:</label>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control" id="emailAddress" name="emailAddress" placeholder="Email Address" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="phoneNumber" class="col-lg-4 control-label">Phone Number:</label>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control" id="phoneNumber" name="phoneNumber" placeholder="Phone Number" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="role" class="col-lg-4 control-label">Privileges</label>
                                    <div class="col-lg-8">
                                    <select name="Role" id="role" class="form-control">
                                        <option value="member" name="privilege">Member</option>
                                        <option value="admin" name="privilege">Admin</option>
                                    </select>
                                    </div>
                                </div>
                                <button class="btn btn-success pull-right" type="submit" name="submit" id="alertMe">Submit</button><br>
                            </form>
                            <!-- end form -->
                        </div>
                        <!-- end modal-body -->
                        
                    </div>
                    <!-- end modal-content -->
                </div>
                <!-- end modal-dialog -->
            </div>
            <!-- end modal -->




</body>

</html>

0 个答案:

没有答案