How to create an object of a class only once in a Multi Step form PHP -
i have class formhandller
<?php include "config.php"; class formhandller { var $dbinstance; var $lastinsertedid;//the id basic information tabel function __construct(){ $this->connectdb(); } function pdomultiinsert($tablename, $data, $pdoobject){ //will contain sql snippets. $rowssql = array(); //will contain values need bind. $tobind = array(); //get list of column names use in sql statement. $columnnames = array_keys($data[0]); //loop through our $data array. foreach($data $arrayindex => $row){ $params = array(); foreach($row $columnname => $columnvalue){ $param = ":" . $columnname . $arrayindex; $params[] = $param; $tobind[$param] = $columnvalue; } $rowssql[] = "(" . implode(", ", $params) . ")"; } //construct our sql statement $sql = "insert `$tablename` (" . implode(", ", $columnnames) . ") values " . implode(", ", $rowssql); //prepare our pdo statement. $pdostatement = $pdoobject->prepare($sql); //bind our values. foreach($tobind $param => $val){ $pdostatement->bindvalue($param, $val); } //execute our statement (i.e. insert data). try { return $pdostatement->execute(); } catch(pdoexception $e) { var_dump($e->getmessage()); //show error error_log($query." :".$e->getmessage(). "\n", 3, getcwd() . "/var/tmp/sql_error.log"); exit; } } private function connectdb(){ try { //create pdo connection $this->dbinstance = new pdo("mysql:host=".dbhost.";dbname=".dbname, dbuser, dbpass); $this->dbinstance->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e) { //show error error_log($query." :".$e->getmessage(). "\n", 3, getcwd() . "/var/tmp/sql_error.log"); exit; } } public function postbasicinformation(){ //add variables coming form . $stmt = $this->dbinstance->prepare('insert basic_information (company,name,designation,email,direct_line,mobile) values (:company, :name, :designation, :email, :directline, :mobile)'); $stmt->execute(array( ':company' => $_post['company'], ':name' => $_post['name'], ':designation' => $_post['designation'], ':email' => $_post['email'], ':directline' => $_post['directline'], ':mobile' => $_post['mobile'], )); $this->lastinsertedid = $this->dbinstance->lastinsertid('id'); //echo $this->lastinsertedid; //$this->dbinstance=null; } public function postprojectawards(){ //an example of adding our "rows" array on fly. for($i=0;$i<sizeof($_post['nominee_company']);$i++){ $rowstoinsert[] = array( 'biid' => $this->lastinsertedid, 'award_type' => 'pa', 'category' => $_post['nominee_category'][$i], 'company' => $_post['nominee_company'][$i], 'name' => $_post['nominee_name'][$i], 'designation' => $_post['nominee_designation'][$i], 'award_title' => $_post['nominee_title'][$i], 'contact' => $_post['nominee_contact'][$i], 'email' => $_post['nominee_email'][$i], 'remark' => $_post['remarks'][$i] ); } //var_dump($rowstoinsert); //call our custom function. $y =$this->pdomultiinsert('nominee', $rowstoinsert, $this->dbinstance); //$this->dbinstance=null; } }
now redirect page
<?php include "controller/formhandller.php"; $x = new formhandller(); if(isset($_post['steps'])){ if($_post['steps']==1){ $x->postbasicinformation(); $url = "nominee.php"; header('location: '.$url); die(); } if($_post['steps']==2){ $x->postprojectawards(); $url = "nominee2.php"; header('location: '.$url); die(); } } else { header('location: '.'index.php'); die(); }
when saving first step using postbasicinformation()
function .
it saves in table called basic_information
, gets id of inserted row , initialize variable
$lastinsertedid
i want use variable in next steps store in other tables. right getting null
any idea
thanks.
i think getting confused life cycle of php script.
anything in xxx.php
lost once script finishes. objects instantiated during execution of xxx.php
lost forever once finishes.
if want preserve information created in xxx.php
use in yyy.php
have save somewhere, either file or database or session or possibly caching system.
i think getting confused. said in comment if want use lastinsertid in script obvious place save between scripts $_session
array
Comments
Post a Comment