Crokom
Crokom

Reputation: 3

PHP Form not submitting data, it just reloads page

I try to submit this form but it just reloads the page with the HTML that was above the $Query code.

The inc/common.php is the PDO Database connection script while inc/header.php is just a place where I store stylesheets.

<?php 
ob_start();

require('inc/common.php'); ?>
<?php include_once "inc/header.php"; ?>
 <body class="register-page">
<div class="register-box">
  <div class="register-logo">
    <a href="../../index2.html">Our<b>Pet</b>.net</a>
  </div>
  <div class="register-box-body">
    <p class="login-box-msg">Register a new membership</p>
<?php   
    $query = " 
        SELECT 
            signupdisabled 
        FROM settings 
    "; 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    $row = $stmt->fetch(); 
    if($row['signupdisabled'] == 1) 
    { 
        ?>
        <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Registrations are currently disabled by Administrators</a>
            </div>
        </div><?php
    }
    else
    {
    if(!empty($_POST)) 
    { 
        if(empty($_POST['username'])) 
        { ?>
                            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Please enter a username.</a>
            </div>
            </div> 
        <?php } die() ?>
         <?php 
        if(empty($_POST['password'])) 
        { ?>
                            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Please enter a password.</a>
            </div>
            </div> 
        <?php } die() ?>
        <?php 


        if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
        { ?>
            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">Invalid eMail Address. Please Try Again.</a>
            </div>
            </div>

        <?php } die() ?>
<?php
        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                username = :username 
        "; 

        $query_params = array( 
            ':username' => $_POST['username'] 
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 

            die("Failed to run query: " . $ex->getMessage()); 
        } 


        $row = $stmt->fetch(); 
        if($row) 
        { ?>
            <div class="container" style="width:450px;">
            <div class="alert alert-danger" style="text-align: center;">
              <a class="alert-link">This username is already in use.</a>
            </div>
            </div>
        <?php } die() ?>
        <?php 
        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                email = :email 
        "; 

        $query_params = array( 
            ':email' => $_POST['email'] 
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
            die("Failed to run query: " . $ex->getMessage()); 
        } 

        $row = $stmt->fetch(); 

        if($row) 
        { 
            die("This email address is already registered"); 
        } 

        $query = " 
            INSERT INTO 'users' ( 
                username, 
                fullname, 
                password, 
                salt, 
                email 
            ) VALUES ( 
                :username, 
                :fullname, 
                :password, 
                :salt, 
                :email 
            ) 
        "; 


        $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 


        $password = hash('sha256', $_POST['password'] . $salt); 

        for($round = 0; $round < 65536; $round++) 
        { 
            $password = hash('sha256', $password . $salt); 
        } 

        $query_params = array( 
            ':username' => $_POST['username'],
            ':fullname' => $_POST['fullname'], 
            ':password' => $password, 
            ':salt' => $salt, 
            ':email' => $_POST['email']     
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
            die("Failed to run query: " . $ex->getMessage()); 
        } 

        header("Location: login.php"); 

        die("Redirecting to login.php"); 

    } 
    ?> 
    <form role="form" action="register.php" method="post">
      <div class="form-group has-feedback">
        <input class="form-control" type="text" class="form-control" name="username" placeholder="Username"/>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input class="form-control" type="text" class="form-control" name="fullname" placeholder="Full Name"/>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>          
      <div class="form-group has-feedback">
        <input class="form-control" type="email" class="form-control" name="email" placeholder="Email" required/>
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input class="form-control" type="password" class="form-control" name="password" placeholder="Password" required/>
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-8">    
          <div class="checkbox icheck">
            <label>
              <input type="checkbox"> I agree to the <a href="#">terms</a>
            </label>
          </div>                        
        </div><!-- /.col -->
        <div class="col-xs-4">
          <button class="btn btn-primary btn-block btn-flat" type="submit">Register</button>
        </div><!-- /.col -->
      </div>
    </form>        

    <a href="login.html" class="text-center">I already have a membership</a>
  </div><!-- /.form-box -->
</div><!-- /.register-box -->
    <?php } ?>

<?php include_once "inc/footer-lr.php"; ?>

Could anyone try to help me please?

Upvotes: 0

Views: 156

Answers (2)

GuiPab
GuiPab

Reputation: 475

Sup man, i sugest you 2 things.

The first one, is like Egzonr says, try to organize a little bit your code, i mean, you doesn't need to create separated files. But try to keep it clean.

The other thing is, create a another file (just to test, you'll delete it after), and try to run your code, but just with your form (remove all of the rest). if it work, you going to add the other pieces of your script

I hope this is usefull for you. Thanks

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

You're using incorrect identifiers for your table name:

INSERT INTO 'users' 
            ^     ^

being regular quotes.

Either remove them

INSERT INTO users

or use ticks

INSERT INTO `users`
  • Having used setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) would have signaled the syntax error.

Add $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened.

Consult:

Identifier Qualifiers:


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Upvotes: 4

Related Questions