2013-07-21 258 views
0

你好我正在创建一个注册类或类,用户填写表单,第一个类创建用户记录,然后它移动到下一个类,它将所有的细节放在user_individual表。然而,iam无法将第一个类中的一个变量传递给另一个类。 第一个类的$ user_id也需要添加到第二个类的末尾。任何想法如何做到这一点?将变量从一个类传递到另一个类

include('../../../Connections/pdo.php'); 

/*reg class*/ 
class newreg { 

private $db; 

public function __construct(){ 
    $this->db = new connection(); 
    $this->db = $this->db->dbconnect(); 
} 

public function addusr($username,$password, 
$usrTypeId,$usrFstRegDate, 
$usrStatus, $activated, 
$chgPwd,$fstActivated, 
$lstlogin,$registered){ 

if(!empty($username)){ 

$st = $this->db->prepare(" INSERT INTO users 
(user_name, user_password, 
user_type_id,user_first_registration_date, 
user_status, activated, 
change_password, first_activated, 
last_log_in, registered) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
$st->bindParam(1, $username); 
      $st->bindParam(2, $password); 
      $st->bindParam(3, $usrTypeId); 
      $st->bindParam(4,$usrFstRegDate); 
      $st->bindParam(5, $user_status); 
      $st->bindParam(6, $activated); 
      $st->bindParam(7, $chgPwd); 
      $st->bindParam(8, $fstActivated); 
      $st->bindParam(9, $lstlogin); 
      $st->bindParam(10, $registered); 

      $st->execute(); 
      echo 'Inserted: '; 
      return $this->db->lastInsertId(); 

     } else { 
    echo "Fail"; 
    } 

    } 
    } 

class individReg { 

private $db; 

public function __construct(){ 
    $this->db = new connection(); 
    $this->db = $this->db->dbconnect(); 
} 


public function addIndivid($title,$firstname,$lastname,$knownAs,$gender,$dob,$nsn,$email, 
$addy1,$addy2,$addy3,$city,$region,$postcode,$pnumber, 
$wnumber,$vquestion,$vanswer,$usr_ind_id,$user_type,$user_id){ 

if(!empty($title)){ 

$st = $this->db->prepare(" INSERT INTO users_individual (
user_individual_title, user_individual_first_name, user_individual_last_name, 
user_individual_known_as,user_gender, user_individual_dob, 
nsn_id, user_individual_email,user_individual_address_line_1,user_individual_address_line_2, 
user_individual_address_line_3,user_individual_city, region, user_individual_postcode, 
user_individual_phone_home, user_individual_phone_work, user_individual_verification_question, 
user_individual_verification_answer,user_individual_id,user_type_id,user_id)  VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");      
$st->bindParam(1, $title); 
$st->bindParam(2, $firstname); 
$st->bindParam(3, $lastname); 
$st->bindParam(4, $knownAs); 
$st->bindParam(5, $gender); 
$st->bindParam(6, $dob); 
$st->bindParam(7, $nsn); 
$st->bindParam(8, $email); 
$st->bindParam(9, $addy1); 
$st->bindParam(10, $addy1); 
$st->bindParam(11, $addy1); 
$st->bindParam(12, $city); 
$st->bindParam(13, $region); 
$st->bindParam(14, $postcode); 
$st->bindParam(15, $pnumber); 
$st->bindParam(16, $wnumber); 
$st->bindParam(17, $vquestion); 
$st->bindParam(18, $vanswer); 
$st->bindParam(19, $usr_ind_id); 
$st->bindParam(20, $user_type); 
$st->bindParam(21, $user_id); 
$st->execute(); 
echo "Success!"; 
} else { 
    echo "Fail"; 
} 
} 
} 

-----------------这就是我所谓的类--------

include('../../../includes/class.php'); 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_user_form")) { 


$username = $_POST['username']; 
$password="defpassword"; 
$usrTypeId=1; 
$usrFstRegDate=date_create()->format('Y-m-d'); 
$usrStatus="registered"; 
$activated=0; 
$chgPwd=0; 
$fstActivated="0000-00-00"; 
$lstlogin=0000-00-00; 
$registered=1; 

$obj = new newreg(); 
$obj->addusr($username, $password,$usrTypeId,$usrFstRegDate,$usrStatus,$activated, 
$chgPwd,$fstActivated,$lstlogin,$registered); 


$title = $_POST['title']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$knownAs = $_POST['knownas']; 
$gender = $_POST['gender']; 
$dob = $_POST['dob']; 
$nsn = $_POST['nsn']; 
$email = $_POST['email']; 
$addy1 = $_POST['addy1']; 
$addy2 = $_POST['addy2']; 
$addy3 = $_POST['addy3']; 
$city = $_POST['city']; 
$region = $_POST['region']; 
$postcode= $_POST['postcode']; 
$pnumber= $_POST['pnumber']; 
$wnumber = $_POST['wnumber']; 
$vquestion = $_POST['vquestion']; 
$vanswer = $_POST['vanswer']; 
$usr_ind_id =1; 
$user_type = 1; 
$user_id =; //how to get the return $this->db->lastInsertId(); and add it to user_id 

$obj2 = new individReg(); 
$obj2->addIndivid($title,$firstname,$lastname,$knownAs,$gender,$dob,$nsn,$email,$addy1,$addy2, 
$addy3,$city,$region,$postcode,$pnumber,$wnumber,$vquestion,$vanswer,$user_type,user_id); 
+0

你的意思是通过'$ user_id'到'$ usr_ind_id' – srain

+0

对不起,我忘了在第二类的末尾添加$ usr_id – user1425394

+0

你怎么使用这两个类? – srain

回答

1

只是使页面newreg :: addusr()函数返回LAST_INSERT_ID 你的情况,这将是:

return $this->db->lastInsertId(); 

将此代码添加到newreg :: addusr函数结束,最终再通过这个ID来individReg :: addIndivid()

+0

这是我一直试图不知道为什么它没有工作的方法。感谢我即将再次尝试。 – user1425394

+0

可能你的请求这些函数的代码如下所示:'$ user_id = newreg :: addusr($ var1,$ var2 ...); individReg :: addIndivid($ user_id,$ var1,...);' – yurkinx

+0

我如何通过返回$ this-> db-> lastInsertId();进入下一课 – user1425394

相关问题