0

Hello i am making a registration class or classes, the user fill out a form the first class makes a user record, then it moves onto the next class which put all their details in the user_individual table. However iam having trouble passing one variable from the first class to the other. The $user_id from the first class also needs to be added to the end of the second class. Any ideas on how to do this??

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";
}
}
}

-----------------This is the page where i call the class--------

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
  • do you mean pass $user_id to $usr_ind_id Commented Jul 21, 2013 at 2:24
  • sorry i forgot to add $usr_id to the end of the second class Commented Jul 21, 2013 at 2:28
  • how do you use both classes? Commented Jul 21, 2013 at 2:30
  • how about this issue? solved? Commented Jul 21, 2013 at 6:57

1 Answer 1

1

Just make newreg::addusr() function to return last_insert_id In your case it will be:

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

Add this code to the end of newreg::addusr function, end then pass this id to individReg::addIndivid()

Sign up to request clarification or add additional context in comments.

4 Comments

this is the approach i had been trying not sure why it didnt work. Thanks tho im going to try it again.
Probably your code that requests those functions will look like this: $user_id=newreg::addusr($var1, $var2...); individReg::addIndivid($user_id, $var1,...);
how do i pass the return $this->db->lastInsertId(); into the next class
Ive updated the code. the user_id is auto incriment and shouldnt have been in first class my bad. now ive put return $this->db->lastInsertId(); at the end of the first class i just need help inputting that intomation into the second class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.