I'm hoping one of you experts can point out the error in my code for my database connection in my PHP. My page wont work at all.
I'm trying to create new users into my database. I've checked all the names in my table and they match up perfectly.
Here is my function:
<?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">×</span></button>
<h4>Success!</h4>
<p>New user registered successfully</p>
</div>
</div>';
die;
}
}
}
?>
Here is my HTML: I call the header.php into my home page but the functions are also called on in there.
<?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">×</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>
</div>