我正在尝试向数据库提交表单,并希望创建一个会话。可以使用的会话是“最后插入的ID”,也可以创建唯一ID作为会话。我面临的问题是,每当我尝试刷新页面时,由于在数据库中插入了新记录,我的sessionID会不断增加。
查看: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>
填写数据后,表单调用 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');
}
和模型:- PostModel.php
function insertToQuoteForm($data) {
$this->db->insert('Quote_Form', $data);
// Return the id of inserted row
return $idOfInsertedData = $this->db->insert_id();
}
提交表单后,它会将数据插入数据库中,并获取数据以将估算出的报价提供给用户。如果用户刷新页面,我想停止在数据库中插入数据或使用会话进行显示。
感谢您的时间!
答案 0 :(得分:1)
首先在模型中检查是否有新记录,然后直接插入,否则返回旧ID。 与此类似:
$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();
答案 1 :(得分:0)
实际上,PHP中有一个预定义的函数称为“最后插入ID”。它可以在PDO和mysqli中使用。在您的php模型中:
function insertToQuoteForm($data) {
if($this->db->insert('Quote_Form', $data)){ //check if successful
return $this->db->lastInsertId(); //return the ID of the last insert
}
}
在您的quote.php中,需要输入以下内容:
$sID = $this->PostModel->insertToQuoteForm($data);
$_SESSION['lastInserted'] = $sID;