I have inserted data into my table using $this->db->insert()
. When the data inserted successfully the message have to display, also the data should not insert again.
我使用$ this-> db-> insert()将数据插入到我的表中。成功插入数据后,必须显示消息,也不应再次插入数据。
controller:
public function index($message = '') {
$this->load->template('homePage');
}
/**
* Insert functionlaity
*/
public function insert() {
if ($_POST['save']) {
$result = $this->home->insertEntry();
$data['message'] = ($result > 0) ? 'saved' : 'Not';
redirect('HomeController',$data);
}
$this->load->view('homePage');
}
Model:
public function insertEntry()
{
$this->name = $_POST['name'];
$this->email = $_POST['email'];
$this->db->insert('users', $this);
return $this->db->affected_rows();
}
View:
<div class="container sampleForm"><?php
echo isset ($message) ? $message : '';
echo form_open( get_class(get_instance()) . '/insert')
. form_label('id') . form_input('id', isset($query['id']) ? $query['id'] : '', 'class="form-control"') . br()
. form_label('name') . form_input('name', isset($query['name']) ? $query['name'] : '', 'class="form-control"') . br()
. form_label('email') . form_custom_input('email','email', isset($query['email']) ? $query['email'] : '', 'class="form-control"') . br()
. form_submit('save', 'save', 'class="btn btn-primary"')
. form_close();
?> </div>
My issue is if i can display status then data insert for every refresh else status will not be displayed.
我的问题是,如果我可以显示状态,则不会显示每个刷新其他状态的数据插入。
This all should works in my HomeController.php as controller, Home.php as model and homePage.php as view file. Not in any other files.
这一切都应该在我的HomeController.php中作为控制器,Home.php作为模型,homePage.php作为视图文件。不在任何其他文件中。
My logic is:
我的逻辑是:
Ex: I have a student table in that i am gonna to store student names and marks like name, m1, m2, m3
. Now I want to add marks if its added i want to display the status. Duplicate insertion should not happen while refresh the page. The student name and marks were similar. I want to do in php alone.
例如:我有一张学生桌,我将存储学生姓名和标记,如名称,m1,m2,m3。现在我想添加标记,如果添加标记我想显示状态。刷新页面时不应发生重复插入。学生姓名和商标相似。我想单独用php做。
5 个解决方案
#1
1
Currently, There is no option to pass the status(success, error) messages to the overview page(or desired page).
目前,没有选项将状态(成功,错误)消息传递到概述页面(或所需页面)。
Use the $_SESSION to holds the values and retrieve values from the session.
使用$ _SESSION保存值并从会话中检索值。
To show the status Message and avoid the duplicate entries, The code as follows:
要显示状态消息并避免重复条目,代码如下:
CONTROLLER:
if (isset($_POST['save'])) {
$result = $this->home->insertEntry();
$message = ($result > 0) ? 'saved' : 'Not';
$this->session->set_userdata('message', $message);
//redirect, avoid duplicate entries
redirect('HomeController');
}
VIEW:
<div class="container sampleForm"><?php
if($this->session->has_userdata('message')){
echo $this->session->message;
$this->session->unset_userdata('message');
}
echo form_open( get_class(get_instance()) . '/insert')
. form_label('id') . form_input('id', isset($query['id']) ? $query['id'] : '', 'class="form-control"') . br()
. form_label('name') . form_input('name', isset($query['name']) ? $query['name'] : '', 'class="form-control"') . br()
. form_label('email') . form_custom_input('email','email', isset($query['email']) ? $query['email'] : '', 'class="form-control"') . br()
. form_submit('save', 'save', 'class="btn btn-primary"')
. form_close();
?> </div>
#2
1
In Controller :
在控制器中:
public function inserttest() {
if (isset($_POST['submit'])) {
$result = $this->admin_model->insertEntry();
if($result>0) {
// values are saved in session to display the message.
$this->session->set_flashdata('message', 'Save');
} else {
$this->session->set_flashdata('message', 'No');
}
// To avoid duplicate redirect to another page
redirect('admin/user/inserttest');
} else {
$this->load->view('admin/test');
}
}
Model:
public function insertEntry()
{
$data=array(
'id'=>$_POST['id'],
'name'=>$_POST['name'],
'email'=>$_POST['email'],
);
$this->db->insert('test_check',$data);
return $this->db->affected_rows();
}
View:
<html>
<body>
<?php if(isset($this->session->flashdata('message'))){
echo $this->session->flashdata('message');
}
?>
<form action="" method="post">
Id:<input type="text" name="id">
Name:<input type="text" name="name">
Email<input type="email" name="email">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
read this for more about flashdata: ellislab.com/codeigniter/user-guide/libraries/sessions.html
阅读本文以获取有关flashdata的更多信息:ellislab.com/codeigniter/user-guide/libraries/sessions.html
#3
0
Simple you can use below code to resolved the duplicate entry for email
很简单,您可以使用下面的代码来解决电子邮件的重复条目
if(!empty($submit))
{
//echo "<pre>"; print_r($_POST); die;
$data = $this->get_data_from_post();
$unique_email = '|is_unique['.TBL_USERS.'.email]';
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[2]|max_length[12]|xss_clean|htmlentities|prep_for_form|alpha');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|htmlentities|prep_for_form'.$unique_email);
if($this->form_validation->run($this) == TRUE ){
$data['name'] = $_POST['name'];
$data['email'] = $_POST['email'];
$res = $this->mdl_customer->_insert($data);
redirect('home'); // where you want to redirect.
}
}
By doing this the user who refreshes will be refreshing landing_page.php which means it won't do the insert twice. best advice: do a check to see if user exists first if so don't insert!
通过执行此操作,刷新的用户将刷新landing_page.php,这意味着它不会执行两次插入操作。最佳建议:如果不插入,请先检查用户是否存在!
Once an insert or update is done in your code you always need to redirect to another page.
在代码中完成插入或更新后,您始终需要重定向到另一个页面。
#4
0
Call this function to open the view page as well as on submit, leave the action=""
it checks that you submit the data or only access the view page, but this data will not inset on refresh
调用此函数打开视图页面以及提交,保留action =“”它检查您是否提交数据或仅访问视图页面,但此数据不会在刷新时插入
function add()
{
if(isset($_POST{['submit']))
{
$result = $this->home->insertEntry();
$data['message'] = $result ? 'saved' : 'Not';
redirect('add',$data);
}
$this->load->view('view page');
}
#5
-1
$result = $this->home->insertEntry();
$data['message'] = $result ? 'saved' : 'Not';
$this->load->view('Your View page name ',$data);
and use the below code in you view page to display message
并在您查看页面中使用以下代码来显示消息
if(isset($message))
{
echo $message;
}
#1
1
Currently, There is no option to pass the status(success, error) messages to the overview page(or desired page).
目前,没有选项将状态(成功,错误)消息传递到概述页面(或所需页面)。
Use the $_SESSION to holds the values and retrieve values from the session.
使用$ _SESSION保存值并从会话中检索值。
To show the status Message and avoid the duplicate entries, The code as follows:
要显示状态消息并避免重复条目,代码如下:
CONTROLLER:
if (isset($_POST['save'])) {
$result = $this->home->insertEntry();
$message = ($result > 0) ? 'saved' : 'Not';
$this->session->set_userdata('message', $message);
//redirect, avoid duplicate entries
redirect('HomeController');
}
VIEW:
<div class="container sampleForm"><?php
if($this->session->has_userdata('message')){
echo $this->session->message;
$this->session->unset_userdata('message');
}
echo form_open( get_class(get_instance()) . '/insert')
. form_label('id') . form_input('id', isset($query['id']) ? $query['id'] : '', 'class="form-control"') . br()
. form_label('name') . form_input('name', isset($query['name']) ? $query['name'] : '', 'class="form-control"') . br()
. form_label('email') . form_custom_input('email','email', isset($query['email']) ? $query['email'] : '', 'class="form-control"') . br()
. form_submit('save', 'save', 'class="btn btn-primary"')
. form_close();
?> </div>
#2
1
In Controller :
在控制器中:
public function inserttest() {
if (isset($_POST['submit'])) {
$result = $this->admin_model->insertEntry();
if($result>0) {
// values are saved in session to display the message.
$this->session->set_flashdata('message', 'Save');
} else {
$this->session->set_flashdata('message', 'No');
}
// To avoid duplicate redirect to another page
redirect('admin/user/inserttest');
} else {
$this->load->view('admin/test');
}
}
Model:
public function insertEntry()
{
$data=array(
'id'=>$_POST['id'],
'name'=>$_POST['name'],
'email'=>$_POST['email'],
);
$this->db->insert('test_check',$data);
return $this->db->affected_rows();
}
View:
<html>
<body>
<?php if(isset($this->session->flashdata('message'))){
echo $this->session->flashdata('message');
}
?>
<form action="" method="post">
Id:<input type="text" name="id">
Name:<input type="text" name="name">
Email<input type="email" name="email">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
read this for more about flashdata: ellislab.com/codeigniter/user-guide/libraries/sessions.html
阅读本文以获取有关flashdata的更多信息:ellislab.com/codeigniter/user-guide/libraries/sessions.html
#3
0
Simple you can use below code to resolved the duplicate entry for email
很简单,您可以使用下面的代码来解决电子邮件的重复条目
if(!empty($submit))
{
//echo "<pre>"; print_r($_POST); die;
$data = $this->get_data_from_post();
$unique_email = '|is_unique['.TBL_USERS.'.email]';
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[2]|max_length[12]|xss_clean|htmlentities|prep_for_form|alpha');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|htmlentities|prep_for_form'.$unique_email);
if($this->form_validation->run($this) == TRUE ){
$data['name'] = $_POST['name'];
$data['email'] = $_POST['email'];
$res = $this->mdl_customer->_insert($data);
redirect('home'); // where you want to redirect.
}
}
By doing this the user who refreshes will be refreshing landing_page.php which means it won't do the insert twice. best advice: do a check to see if user exists first if so don't insert!
通过执行此操作,刷新的用户将刷新landing_page.php,这意味着它不会执行两次插入操作。最佳建议:如果不插入,请先检查用户是否存在!
Once an insert or update is done in your code you always need to redirect to another page.
在代码中完成插入或更新后,您始终需要重定向到另一个页面。
#4
0
Call this function to open the view page as well as on submit, leave the action=""
it checks that you submit the data or only access the view page, but this data will not inset on refresh
调用此函数打开视图页面以及提交,保留action =“”它检查您是否提交数据或仅访问视图页面,但此数据不会在刷新时插入
function add()
{
if(isset($_POST{['submit']))
{
$result = $this->home->insertEntry();
$data['message'] = $result ? 'saved' : 'Not';
redirect('add',$data);
}
$this->load->view('view page');
}
#5
-1
$result = $this->home->insertEntry();
$data['message'] = $result ? 'saved' : 'Not';
$this->load->view('Your View page name ',$data);
and use the below code in you view page to display message
并在您查看页面中使用以下代码来显示消息
if(isset($message))
{
echo $message;
}