0

its my first time here i have a question im making a register form but i need to insert to 2 databases here is my code.

when i run its only register in one database sometimes db1 other times db2

-Sorry for my bad english

-Here's register.php

require_once 'config/dbconfig.php';

if($user->is_loggedin()!="")
{
    $user->redirect('home.php');
}

if(isset($_POST['btn-signup']))
{
   $uname = trim($_POST['txt_uname']);
   $umail = trim($_POST['txt_umail']);
   $upass = trim($_POST['txt_upass']); 
   $rpass = trim($_POST['txt_rpass']);  
   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"; 
   }

   elseif($upass != $rpass){
         $msg = "passwords doesn't match";
    }
   else
   {
      try
   {
         $stmt = $DB_con->prepare("SELECT user_name,user_email FROM users                                WHERE user_name=:uname OR user_email=:umail");
         $stmt2 = $DB_con2->prepare("SELECT user_name,user_email FROM users     WHERE user_name=:uname OR user_email=:umail");
         $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
         $stmt2->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($fname,$lname,$uname,$umail,$upass)) 
        {
            $user->redirect('register.php?joined');
        }
     }
 }
 catch(PDOException $e)
 {
    echo $e->getMessage();
     }
  } 
}

-Here Class.User.php

  <?php
  class USER
  {
private $db;

function __construct($DB_con)
{
  $this->db = $DB_con;
}

public function register($fname,$lname,$uname,$umail,$upass)
{
   try
   {
       $new_password = password_hash($upass, PASSWORD_DEFAULT);

       $stmt = $this->db->prepare("INSERT INTO users(user_name,user_email,user_pass) 
                                                   VALUES(:uname, :umail, :upass)");

       $stmt->bindparam(":uname", $uname);
       $stmt->bindparam(":umail", $umail);
       $stmt->bindparam(":upass", $new_password);            
       $stmt->execute(); 

       return $stmt; 
   }
   catch(PDOException $e)
   {
       echo $e->getMessage();
   }    
}

public function login($uname,$umail,$upass)
{
   try
   {
      $stmt = $this->db->prepare("SELECT * FROM users WHERE user_name=:uname OR user_email=:umail LIMIT 1");
      $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
      $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
      if($stmt->rowCount() > 0)
      {
         if(password_verify($upass, $userRow['user_pass']))
             {
            $_SESSION['user_session'] = $userRow['user_id'];
            return true;
         }
         else
         {
            return false;
         }
      }
   }
   catch(PDOException $e)
   {
       echo $e->getMessage();
   }
   }

   public function is_loggedin()
     {
                if(isset($_SESSION['user_session']))
        {
           return true;
        }
     }

         public function redirect($url)
     {
         header("Location: $url");
     }

         public function logout()
        {
          session_destroy();
         unset($_SESSION['user_session']);
    return true;
     }
    }
  ?>

-and the DBConfig.php

 <?php
 session_start();

 $DB_host = "localhost";
 $DB_user = "root";
 $DB_pass = "";

 try
 {
      $DB_con = new PDO("mysql:host=     {$DB_host};dbname=dblogin",$DB_user,$DB_pass);
 $DB_con2 = new PDO("mysql:host=     {$DB_host};dbname=dblogin2",$DB_user,$DB_pass);
      $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $DB_con2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 }
 catch(PDOException $e)
 {
 echo $e->getMessage();
 }


 include_once 'class.user.php';
 $user = new USER($DB_con);
 $user2 = new USER($DB_con2);
FenixTM
  • 9
  • 2
  • Use transactions. This will ensure that either both inserts succeed or neither. More here: http://php.net/manual/en/pdo.begintransaction.php – Rehmat Feb 23 '16 at 06:14
  • You have to add the code in `$user->register() method` to your post so we can see what's happening in there... – EhsanT Feb 23 '16 at 07:03
  • From what I can see in your `Class.User.php` file, you are only inserting data in `register method` for `$user` object which is on first database and you are not registering the user on the second database at all. then in `DBConfig.php` file you have this line at the end: `$user2 = new USER2($DB_con2);`, I wonder if you have a class named `USER2`! does this line generates any error?? shouldn't it be like this: `$user2 = new USER($DB_con2);` – EhsanT Feb 23 '16 at 23:28

0 Answers0