使用PDO将1个表单连接到2个Mysql数据库

时间:2016-02-23 05:28:16

标签: php mysql pdo multiple-databases

这是我第一次来这里,我有一个问题,我正在制作一个注册表格,但我需要插入到2个数据库这里是我的代码。

当我在一个数据库中运行它的唯一寄存器时有时db1其他时间db2

- 抱歉我的英语不好

-Here的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;
     }
    }
  ?>

- 和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);

0 个答案:

没有答案