如何将表单中的数据插入数据库并获取最后插入的ID/在CI中创建uniqueID作为用户的Session
How to insert the data from the form to the database and get the last inserted ID / create uniqueID as the user's Session in CI
我正在尝试向数据库提交表单并想创建一个会话。可以使用的会话是 Last Inserted ID 或创建 uniqueID 作为会话。我面临的问题是,每当我尝试刷新页面时,我的 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();
}
提交表单后,它会将数据插入数据库并获取数据以将计算出的估计报价返回给用户。如果用户刷新页面,我想要么停止向数据库中插入数据,要么使用会话来显示。
提前感谢您抽出时间!
模型中先检查,如果有新记录则直接插入else return旧的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();
实际上,PHP 中有一个名为 "last insert 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;
我正在尝试向数据库提交表单并想创建一个会话。可以使用的会话是 Last Inserted ID 或创建 uniqueID 作为会话。我面临的问题是,每当我尝试刷新页面时,我的 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();
}
提交表单后,它会将数据插入数据库并获取数据以将计算出的估计报价返回给用户。如果用户刷新页面,我想要么停止向数据库中插入数据,要么使用会话来显示。
提前感谢您抽出时间!
模型中先检查,如果有新记录则直接插入else return旧的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();
实际上,PHP 中有一个名为 "last insert 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;