andreaem
andreaem

Reputation: 1655

PHP: Registration form don't send to mySQL database

I got a registration form that currently not send data or the function isn't work as expected.

First page got all the form that must to be sent to sign-up.php using POST method, all the user input will be striped and if all the request form data is putted by user will be sent to method registerin USER class. The class handle the mysql submission.

The database connection work as expected.

HTML Form

<form id="signupform" class="form-horizontal" role="form" action="sign-up.php" method="POST">

   <div id="signupalert" style="display:none" class="alert alert-danger">
      <p>Errore:</p>
   </div>
   <div class="form-group">
       <label for="email" class="col-md-3 control-label">Email</label>
       <div class="col-md-9">
           <input type="text" class="form-control" name="email" placeholder="Indirizzo e-Mail">
       </div>
   </div>
   <div class="form-group">
       <label for="username" class="col-md-3 control-label">Username</label>
       <div class="col-md-9">
           <input type="text" class="form-control" name="username" placeholder="Username">
       </div>
   </div>
   [ ... Striped Code ...]                         
   <div class="col-md-offset-3 col-md-9">
        <input type="submit" name="submit" id="submit" type="button" class="btn btn-info" value="Registrati"/>
        <button id="btn-fbsignup" type="button" class="btn btn-primary"><i class="glyphicon glyphicon-facebook"></i>Registrati con Facebook</button>
   </div>
</form>

sign-up.php

<?php
session_start();
require_once('core/class.user.php');
$user = new USER();
var_dump($_POST);
if($user->is_loggedin()!="")
{
    $user->redirect('home.php');
}

if(isset($_POST['submit']))
{
    $uname       = strip_tags($_POST['username']);
    $umail       = strip_tags($_POST['email']);
    $upass       = strip_tags($_POST['password']);
    $firstname   = strip_tags($_POST['firstname']);
    $lastname    = strip_tags($_POST['lastname']);
    $address     = strip_tags($_POST['address']);
    $cap         = strip_tags($_POST['cap']);
    $city        = strip_tags($_POST['city']);
    $prov        = strip_tags($_POST['prov']);
    $tel         = strip_tags($_POST['tel']);

    if($uname=="")   {
        $error[] = "provide username !";
    }
    else if($umail=="")  {
        $error[] = "provide email id !";
    }
    else if(!filter_var($umail, FILTER_VALIDATE_EMAIL))  {
        $error[] = 'Please enter a valid email address !';
    }
    else if($upass=="")  {
        $error[] = "provide password !";
    }
    else if(strlen($upass) < 6){
        $error[] = "Password must be atleast 6 characters";
    }
    else
    {
        try
        {
            $stmt = $user->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:uname OR user_email=:umail");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $row=$stmt->fetch(PDO::FETCH_ASSOC);

            if($row['user_name']==$uname) {
                $error[] = "sorry username already taken !";
            }
            else if($row['user_email']==$umail) {
                $error[] = "sorry email id already taken !";
            }
            else
            {
                if($user->register($uname,$umail,$upass,$firstname,$lastname,$address,$cap,$city,$prov,$tel)){
                    $user->redirect('sign-up.php?joined');
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}
if ($_GET['joined']) {
    print 'Registrazione completata';
}
?>

class.user.php

class USER
{

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function register($uname, $umail, $upass, $firstname, $lastname, $address, $cap, $city, $prov, $tel)
    {
        try {

            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass,disk_quota, details_nome, details_cognome, details_indirizzo, details_cap, details_citta, details_provincia, details_telefono) 
                                                   VALUES(:uname, :umail, :upass, '209715200', :firstname, :lastname, :address, :cap, :city, :prov, :tel)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);
            $stmt->bindparam(":firstname", $firstname);
            $stmt->bindparam(":lastname", $lastname);
            $stmt->bindparam(":address", $address);
            $stmt->bindparam(":cap", $cap);
            $stmt->bindparam(":city", $city);
            $stmt->bindparam(":prov", $prov);
            $stmt->bindparam(":tel", $tel);

            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    [... striped rest of class code ...]
    public function is_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
           return true;
        }
    }
  }
  [... striped rest of class code ...]

dbconfig.php

<?php
class Database
{   
   private $host = "localhost";
   private $db_name = "c9";
   private $username = "andreaem_dev";
   private $password = "";
   public $conn;

   public function dbConnection()
   {

       $this->conn = null;    
       try
       {
           $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8", $this->username, $this->password);
           $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
      }
   }

MySQL log

MySQL log return only the initial connection to database

170314  4:09:26   163 Connect   andreaem_dev@localhost on c9
163 Quit    

var_dump() of POST data

/home/ubuntu/workspace/auth/sign-up.php:5: array(12) { 'email' => string(19) "[email protected]" 'username' => string(7) "johndoe" 'firstname' => string(4) "John" 'lastname' => string(3) "Doe" 'address' => string(15) "Fantasy Road,14" 'cap' => string(6) "010101" 'city' => string(4) "Roma" 'prov' => string(4) "Roma" 'tel' => string(13) "+395551234567" 'passwd' => string(8) "jdoe1234" 'repasswd' => string(8) "jdoe1234" 'submit' => string(10) "Registrati" }

Behavior When the form is submitted to sign_up.phpI got a blank page, except for the var_dump() print-out. Nothing where inserted in database and no errors shown

I hope that I've put all the needed to understand where the problem is.

Upvotes: 0

Views: 138

Answers (2)

andreaem
andreaem

Reputation: 1655

Thanks to CodeGodie help, I've found that the problem is in the IFs blocks located at sign_up.php page, removing this the code work as expected and the user is created.

I will manage that using jQuery that is more user friendly too. Thanks

Upvotes: 1

Kerkouch
Kerkouch

Reputation: 1456

You have an error in this statement if($user->is_loggedin()!="") because true!="" was true if the user logged in, then you are redirected to the home.php, replace it with this one:

if(!$user->is_loggedin())

Upvotes: 0

Related Questions