Reputation: 370
I am trying to submit a form to the database and want to create a session. A session that can be used is either the Last Inserted ID or create uniqueID as a session. The issue that what I am facing is, whenever I am trying to refresh the page, my sessionID keeps incrementing because new record was inserted in the database.
View: Welcome_Message.php
<form method="POST" action="GetQuotes/quote" id="ink-form" name="ink-form">
<ul class="inkappform inkbookform">
<li class="textheading ink-ff-row"><h2><span class="msg_text">To get a quote, select your vehicle type</span></h2></li>
<li class = "textfname ink-ff-row"><input type = "text" name = "bk_name" class = "inktext inklarge" placeholder = "First name" required = "" autocomplete="off" /></li>
<li class = "textlname ink-ff-row"><input type = "text" id = "bk_lname" name = "bk_lname" class = "inktext inklarge" placeholder = "Last name" autocomplete="off" /></li>
<li class = "textaptemail ink-ff-row"><input type = "email" name = "bk_email" class = "inktext inklarge" placeholder = "Email" required = "" autocomplete="off" /></li>
<li class = "textaptemail ink-ff-row"><input type = "text" name = "bk_contact" class = "inktext inklarge" placeholder = "Contact number" required = "" autocomplete="off" /></li>
<li class = "textaptphone ink-ff-row"><input type = "text" name = "pick_add" list="pu_postcode" id = "pick_add" class = "inktext inklarge" placeholder = "Type Pickup Posctcode" required = "" autocomplete="off" />
<datalist id="pu_postcode">
<option value=“001”>001 </option>
<option value=“002”>002 </option>
<option value=“003”>003 </option>
<option value=“004”>004 </option>
</datalist> </li>
<li class = "textaptphone ink-ff-row"><input type = "text" name = "drop_add" list="do_postcode" id = "drop_add" class = "inktext inklarge" placeholder = "Type Dropoff Postcode" required = "" autocomplete="off" />
<datalist id="do_postcode">
<option value=“001”>001 </option>
<option value=“002”>002 </option>
<option value=“003”>003 </option>
<option value=“004”>004 </option>
</datalist> </li>
<li class = "textaptphone ink-ff-row">
<input type = "text" name = "car_list" list="car_dalalist" id = "car_list" class = "inktext inklarge" placeholder = "Type of Car" required = "" autocomplete="off" />
<datalist id="car_dalalist">
<option value=“BMW”>BMW </option>
<option value=“Merc”>Merc </option>
<option value=“Honda”>Honda </option>
<option value=“Toyota”>Toyota </option>
</datalist> </li>
<li class = "textaptphone ink-ff-row">
<input type = "text" name = "car_models" list="car_model_dalalist" id = "car_models" class = "inktext inklarge" placeholder = "Car Model" required = "" autocomplete="off" />
<datalist id="car_model_dalalist">
// Car Models list populates
</datalist> </li>
<li class = "textaptphone ink-ff-row">
<input type = "text" name = "car_man_year" list="car_year_dalalist" id = "car_man_year" class = "inktext inklarge" placeholder = "Manufacturing Year" required = "" autocomplete="off" />
<datalist id="car_year_dalalist">
//Manufacturing list populates
</datalist> </li>
<li class = "select_item ink-ff-row">
<input type = "text" name = "bodytypeselect" list="car_bodytype_dalalist" id = "bodytypeselect" class = "inktext inklarge" placeholder = "Body Type" required = "" autocomplete="off" />
<datalist id="car_bodytype_dalalist">
<select id="cbody"></select>
</datalist>
</li>
<input type="submit" name="estimate" id="submit" class='ink-submit inkrequired' value="Estimate Quote"/>
</ul>
</form>
After filling the data, the form call GetQuotes/quote
public function quote()
{
if (!empty($_POST)) {
$first_name = $this->input->post('bk_name');
$last_name = $this->input->post('bk_lname');
$email = $this->input->post('bk_email');
$contact = $this->input->post('bk_contact');
$p_address = $this->input->post('pick_add');
$d_address = $this->input->post('drop_add');
$carList = $this->input->post('car_list');
$carModel = $this->input->post('car_models');
$carbodytype = $this->input->post('bodytypeselect');
$manYear = $this->input->post('car_man_year');
$sessionID = $this->input->post('SessID');
$btn = $this->input->post('estimate');
$this->session->set_flashdata('fname', $first_name);
$this->session->set_flashdata('lname', $last_name);
$this->session->set_flashdata('pick_pc', $p_address);
$this->session->set_flashdata('drop_pc', $d_address);
$this->session->set_flashdata('carMake', $carList);
$this->session->set_flashdata('carModel', $carModel);
$this->session->set_flashdata('carSize', $carbodytype);
$this->session->set_flashdata('ManYear', $manYear);
if ($first_name && $last_name && $p_address && $d_address && $carList) {
// Loading model
$data = array('First_Name'=> $first_name, 'Last_Name' => $last_name, 'Email' => $email, 'Phone' => $contact, 'Origin' => $p_address, 'Destination' => $d_address, 'CarMake' => $carList,
'CarModel' => $carModel, 'ManYear' => $manYear, 'CarType' => $carbodytype, 'SessionID' => $sessionID );
$sID = $this->PostModel->insertToQuoteForm($data);
}
}
$data = $this->getNewQuoteNumber();
$this->load->view('insertNewQuote', $data);
}
function getNewQuoteNumber () {
$data = array();
$this->session->keep_flashdata('QuoteNo');
$qNo = $this->session->flashdata('QuoteNo');
$first_name = $this->session->flashdata('fname');
$last_name = $this->session->flashdata('lname');
$p_address = $this->session->flashdata('pick_pc');
$d_address = $this->session->flashdata('drop_pc');
$carList = $this->session->flashdata('carMake');
$carModel = $this->session->flashdata('carModel');
$carbodytype = $this->session->flashdata('carSize');
$manYear = $this->session->flashdata('ManYear');
echo 'qNo is:: '. $qNo ;
echo '<br> Fname is:: '. $first_name ;
echo '<br> CarType is:: '. $carbodytype ;
$data['quotes_fetched'] = $this->PostModel->getQuotes(); // load the view file , we are passing $data array to view file
$data['withgoods_quotes_fetched'] = $this->PostModel->withGoodsGetQuotes();
$data['rowstotal'] = count($data['quotes_fetched']);
//$this->PostModel->insertToQuoteForm();
$this->load->view('page_header');
$this->load->view('page_menu');
$this->load->view('QueryHandler/Quote', $data);
$this->load->view('page_footer');
}
And Model:- PostModel.php
function insertToQuoteForm($data) {
$this->db->insert('Quote_Form', $data);
// Return the id of inserted row
return $idOfInsertedData = $this->db->insert_id();
}
When the form is submitted, it inserts the data in the database as well as Fetches the data to give the estimated quote calculated back to the users. If the user refreshes the page, I want to either stop inserting the data in the database or use a session to display.
Thanks for your time in advance!
Upvotes: 0
Views: 83
Reputation: 374
Actually, there is a predefined function in PHP called "last insert id". It can be used in PDO and mysqli. Inside your php model:
function insertToQuoteForm($data) {
if($this->db->insert('Quote_Form', $data)){ //check if successful
return $this->db->lastInsertId(); //return the ID of the last insert
}
}
And inside your quote.php, you need to put this:
$sID = $this->PostModel->insertToQuoteForm($data);
$_SESSION['lastInserted'] = $sID;
Upvotes: 0
Reputation: 795
In the model check first, if new record then inserts directly else return the old id. Something similar to this :
$temp = $data;
unset($temp['SessionID']);
$this->db->where($temp);
$query = $this->db->get('Quote_Form');
if ($query->num_rows() > 0) {
$result = $query->result();
return $idOfInsertedData = $result->id;
}
$this->db->insert('Quote_Form', $data);
return $idOfInsertedData = $this->db->insert_id();
Upvotes: 1