public function myfuntion() {
if($_POST){
$urnno = "NU-62819100";
$data = array(
"name" => $this->input->post('name'),
"email" => $this->input->post('email'),
"phone" => $this->input->post('phone'),
"urnno" => $urnno + 1
);
$sql = $this->db->insert('student',$data);
if($sql){
echo "success";
}else{
echo "fail";
}
}
$this->loca->view('myform');
}
在这段代码中,我只是插入表单值,但是当我插入值时,urnno
必须在新插入后自动递增,就像在我第一次插入值时,urnno
必须是{ {1}}第二次是NU-62819101
,然后是102
,就像这样。那么,我该怎么做?请帮助我。
谢谢
答案 0 :(得分:0)
您可以首先获取存储在urnno
中的最后一个table
,使其数字部分递增,然后将其保存在table
中。
我已经为您的问题写了一个可能的解决方案,并在必要时提及您的评论。看看是否对您有帮助。
public function myfuntion() {
if($_POST){
// get the last row saved in table
$last = $this->db->select('urnno')->from('student')->order_by('id', 'DESC')->get()->row(); // id or some other auto incremented field
// get urnno from the last row
$last_urnno = $last->urnno;
$last_arr = explode("-", $last_urnno); // make it an array to get the numerical part
$new = $last_arr[1] + 1; // increment the value
$urnno = "NU-{$new}"; // new value
$data = array(
"name" => $this->input->post(''),
"email" => $this->input->post(''),
"phone" => $this->input->post(''),
"urnno" => $urnno // new value
);
$sql = $this->db->insert('student', $data);
if($sql){
echo "success";
}else{
echo "fail";
}
}
$this->loca->view('myform');
}
答案 1 :(得分:0)
如果仅将列的值递增1,则可以尝试在数据库中设置自动递增的列。它使操作更加轻松,并消除了不必要的计算。