I am trying to create a Registration form with PHP and PDO. When I hit submit on my registration form, my PHP page refreshes, my inputs erase and my typed text gets added to my URL; however, no messages print and nothing gets added to my database.
code for database.php (I tried putting the port in the newPDO, but it produced an error and my page would not load)
<?php
$server = 'localhost';
$username = 'my_cPanel_username';
$password = 'my_cPanel_password';
$database = 'MySQL_database';
try{
$conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
die( "Connection failed: " . $e->getMessage());
}
?>
This is the PHP code at the top of my page_registration_test.php (initially I did not have the error_reporting(E_ALL)
and the print_r($sth->errorInfo
code in there, but I was trying to search for ways to identify what was wrong, so I added them in places I think StackOverFlow users recommended. I also added if (isset($_POST['submit']))
because I thought maybe my button was not triggering anything?
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
if( isset($_SESSION['user_id']) ){
header("Location: index.html");
exit;
}
require 'database.php';
$message = '';
if (isset($_POST['submit'])) {
// Making sure my form inputs are not empty
if(!empty($_POST['full_name']) && !empty($_POST['email']) && !empty($_POST['office_name']) && !empty($_POST['office_phone']) && !empty($_POST['password']) && !empty($_POST['terms'])) {
// Enter the new user in the database
$sql = "INSERT INTO members (sign_up_date, full_name, email, office_phone, office_phone, password, terms) VALUES (NOW(), :full_name, :email, :office_name, :office_phone, :password, :terms)";
$stmt = $conn->prepare($sql);
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$office_name = $_POST['office_name'];
$office_phone = $_POST['office_phone'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$terms = $_POST['terms'];
$stmt->bindParam(':full_name', $full_name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':office_name', $office_name);
$stmt->bindParam(':office_phone', $office_phone);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':terms', $terms);
if( $stmt->execute() ) {
$message = 'You have successfully created a new user account, full access will be granted within 24 hours.';
}
else {
$message = 'Sorry there must have been an issue creating your account, please try again or contact us.';
}
print_r($sth->errorInfo());
}
}
?>
Form code from the body of my site...
<form class="reg-page" id="register" name="members">
<div class="reg-header" method="post" action="page_registration_test.php">
<h2>Register a new account</h2>
<p>Already Signed Up? Click <a href="page_login.html" class="color-green">Sign In</a> to login to your account. All new accounts will be verified within 24 hours before access to service is allowed. Account verification is needed for this free service due to how our music licenses are constructed.</p>
</div>
<label>Full Name <span class="color-red">*</span></label>
<input type="text" name="full_name" placeholder="Full Name" class="form-control margin-bottom-20">
<label>Email Address <span class="color-red">*</span></label>
<input type="text" name="email" placeholder="example@domain.com" class="form-control margin-bottom-20">
<label>Office or Practice Name <span class="color-red">*</span></label>
<input type="text" name="office_name" placeholder="Office Name" class="form-control margin-bottom-20">
<label>Office or Practice Phone Number <span class="color-red">*</span></label>
<input type="text" name="office_phone" placeholder="1234567890" class="form-control margin-bottom-20">
<div class="row">
<div class="col-sm-6">
<label>Password <span class="color-red">*</span></label>
<input type="password" name="password" id="txtNewPassword" placeholder="Password" class="form-control margin-bottom-20">
</div>
<div class="col-sm-6">
<label>Confirm Password <span class="color-red">*</span></label>
<input type="password" id="txtConfirmPassword" placeholder="Confirm Password" class="form-control margin-bottom-20" onChange="checkPasswordMatch();">
</div>
<div class ="registrationFormAlert" id="divCheckPasswordMatch">
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-6 checkbox">
<label>
<input type="checkbox" name="terms" value="Y">
I read <a href="page_terms.html" class="color-green">Terms and Conditions</a>
</label>
</div>
<div class="col-lg-6 text-right">
<input type="submit" id="register" name="submit" value="Register">
</div>
</div>
</form>
I do have this snippet of code at the top of my page beneath my header, but it seems to produce absolutely nothing as I've never had a message display. I was hoping to not redirect to a different page if logged in, but simply post a message. But it doesn't matter really, as long as the form submits:
<?php if(!empty($message)): ?>
<p><?= $message ?></p>
<?php endif; ?>
And I believe my database table is formed properly:
Any and all help would be greatly appreciated for this poor guy who is apparently in over his head!!!
Edit: Now my page at least creates an error, "Sorry there must have been an issue creating your account, please try again or contact us" after fixing my code (thanks everyone!!!!!)
my form is now accurately coded as:
<form class="reg-page" id="register" name="members" method="post" action="page_registration_test.php">
But my form still does not properly post data to my MySQL Database? Does anyone know if I properly coded the checkbox
for the terms of service? And the Now()
date stamp for registration day?