I'm trying to insert data to a MySQL table with PHP. I have a form that I would like to post form data like username and password. Everything seems to go fine but the data does not get posted to the database. My database connection is simple... Here it is from a file called db_connection.php
:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "");
define("DB_PASS", "");
define("DB_NAME", "global_gl");
// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
The page that has the form is called newUser.php
and it posts to itself. Here's the page:
<?php
require_once("/includes/session.php");
require_once("includes/db_connection.php");
require_once("includes/functions.php");
require_once("includes/validation_functions.php");
?>
<?php
if (isset($_POST['submit'])) {
// Process the form
// validations
$required_fields = array("firstName", "lastName", "email", "username", "password");//TODO: add country and birthday to this after debugging
validate_presences($required_fields);
$fields_with_max_lengths = array("username" => 30);
validate_max_lengths($fields_with_max_lengths);
if (empty($errors)) {
// Perform Create
$firstName = mysql_prep($_POST["firstName"]);
$lastName = mysql_prep($_POST["lastName"]);
$email = mysql_prep($_POST["email"]);
$username = mysql_prep($_POST["username"]);
$hashed_password = mysql_prep($_POST["password"]);
$query = "INSERT INTO useradmin (";
$query .= " firstName, lastName, email, username, hashed_password, ";
$query .= ") VALUES (";
$query .= " '{$firstName}','{$lastName}','{$email}','{$username}','{$hashed_password}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["message"] = "User created.";
redirect_to("manage_admin.php");//TODO: After cretion choose a page to return to.
} else {
// Failure
$_SESSION["message"] = "User creation failed.";
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
<?php include("includes/layouts/header.php"); ?>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="scripts/bday-picker.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#createUserDatePicker").birthdaypicker({});
});
</script>
</head>
<div id="main">
<div id="navigation">
<?php// echo navigation($current_subject, $current_page); ?>
</div>
<div id="page">
<form action="newUser.php" method ="post">
<p>First Name:
<input type = "text" name ="firstName" value=""/>
</p>
<p>Last Name:
<input type = "text" name ="lastName" value=""/>
</p>
<p>Email:
<input type = "email" name ="email" value=""/>
</p>
<p>Username:
<input type = "username" name ="username" value=""/>
</p>
<p>Password:
<input type = "password" name ="password" value=""/>
</p>
Counntry:
<?php
include("countrySelect/index.html");
?><br>
</p>
<p>Birthday:
<div class="picker" id="createUserDatePicker"></div>
</p>
<p>
<input type = "radio" name ="contactMe" value="0"/> Contact me with news and exclusive conent about Global Gaming League and West Coast Chill products.
</p>
<p>
<input type = "checkbox" name ="accept" value="0"/> I accept the terms of service and privacy policy.
</p>
<p>
<input type="submit" name="submit" value="Create User" />
</p>
</form>
<p>
<a href ="createUser.php">Cancel</a> <!--Note: this should take us back to a previous page
</p>
</div>
</div>
<?php include("includes/layouts/footer.php"); ?>
When I click on the submit button, the form fields are cleared and nothing is saved to the database. Can you please help with determining why data is not posted? Thanks!