0

您好,我正在注册一个或多个课程,用户填写表格,第一个课程制作用户记录,然后进入下一个课程,将所有详细信息放入 user_individual 表中。但是,我无法将一个变量从第一类传递到另一个。第一类的 $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);
4

1 回答 1

1

只需使 newreg::addusr() 函数返回 last_insert_id 在您的情况下,它将是:

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

将此代码添加到 newreg::addusr 函数的末尾,然后将此 id 传递给 individReg::addIndivid()

于 2013-07-21T02:25:39.307 回答