0

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:

cPanel screenshot

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?

  • first of all put `session_start()` on the very first line of the page before any possible output occurs due to error or anything. – BetaDev Jan 04 '17 at 20:23
  • put `method="post" action="page_registration_test.php` inside your form tag not inside div tag – BetaDev Jan 04 '17 at 20:26
  • Amazing!!! Why would I put `method="post"` and `action="page_registration_test.php"` inside my div instead of my form, who knows?!? Thanks! – Confused_In_Ohio Jan 04 '17 at 21:14

3 Answers3

4

and my typed text gets added to my URL

You're looking for values in $_POST, but your form is using GET. Set the form method to POST instead:

<form class="reg-page" id="register" name="members" method="POST">

Conversely, you could change your code to use $_GET instead of $_POST, but I would consider that to be a less ideal approach.

You'll notice when using POST that the values are not in the URL. They are instead in the "body" of the request, which isn't shown in browsers by default but you can examine it in your browser's debugging tools. This is generally preferred for forms as it doesn't clutter the URL with lots of data.

(For reference, one main reason often cited for using GET is that it can be linked directly whereas a POST can not. This is rarely a need for form submissions, though.)

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Also worth mentioning: `GET` requests may show up in plain text in the server logs, and while refreshing the browser after a `GET` this will re-send the same request again, which could have undesired consequences if performing an action such as a user registration. With `POST` the user will at least be prompted to re-send the data (unless you do Post/Redirect/Get). – Mike Jan 04 '17 at 20:35
0

The default method for forms are GET, you must specify it:

<form class="reg-page" id="register" name="members" method="post">

Read more

Community
  • 1
  • 1
ka_lin
  • 9,329
  • 6
  • 35
  • 56
0

Turns out I'm a moron!!!!!

     $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)";

I have 'office_phone, office_phone' instead of 'office_name, office_phone'

Man... 3 late nights for something so simple!

P.S., I did not catch this, I was questioning SiteGround about maybe a server side issue? Their customer service agent reviewed my code, inserted:

print_r($stmt->errorInfo());

Which I have mis-syntaxed in my above code, corrected my coding issue, and confirmed it was writing properly to my database... thank you SiteGround!!! (Am I allowed to thank them on here?) I should have asked them days ago... or simply coded correctly to begin with :(