使用Code Igniter中的Last Insert Id生成唯一代码?

时间:2017-12-19 10:47:24

标签: php mysql codeigniter

我需要使用指定格式的最后一个插入ID创建 drcode , 以前我用cor php来获取代码,但现在在codeigniter中我无法获得前一个代码。我怎样才能做到这一点?我正在提供我的控制器和型号

控制器

public function newdoctor_post() {
            $employee_id = $this->post('EMPLOYEE_ID');
            $doctorname = $this->post('DOCTOR_NAME');
            $mobilenumber = $this->post('DRMOBILE');
            $users_data = $this->Rest_user_model->doctor_exists($mobilenumber);
            if ($users_data == 1) {
                $message = ['status' => 2,
                // 'result' => array(),
                'message' => 'Doctor already registered'];
            } else {
                $speciality = $this->post('SPECIALITY');
                $longitude = $this->post('LONGITUDE');
                $latitude = $this->post('LATITUDE');
                $drcode = $this->post('DRCODE');
                $createdon = date('Y-m-d H:i:s');
                $insert_array = array('EMPLOYEE_ID' => $employee_id, 'DOCTOR_NAME' => $doctorname, 'DRMOBILE' => $mobilenumber, 'SPECIALITY' => $speciality, 'LONGITUDE' => $longitude, 'LATITUDE' => $latitude, 'CREATEDON' => $createdon);
                $users_data = $this->Rest_user_model->doctorregistration($insert_array);
                $message = ['status' => 1, // 'result' => $users_data,
                'message' => 'Doctor Registered Successfully'];
            }
            $this->set_response($message, REST_Controller::HTTP_OK);
        }

模型

public function doctorregistration($data) {
        if (!empty($data)) {
            $this->db->insert('DOCTORDETAILS', $data);
            return $this->db->insert_id();
        }
        return 0;
    }

代码生成

$sql1="SELECT DRCODE FROM DOCTORDETAILS  ORDER BY DRCODEDESC LIMIT 1";
    $query=mysql_query($sql1);
    if (!$sql1) { // add this check.
    die('Invalid query: ' . mysql_error());
    }
    $output_array2=array();
    while($row=mysql_fetch_assoc($query))
    {
    $ids=$row['DRCODE'];

    }
    // echo $ids;
    if($ids){
        $su=1;
        $num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
        $unique=$num;
    }else{
        $unique='DR000001';     
    }

2 个答案:

答案 0 :(得分:0)

试试这个 - 将以下代码替换为代码生成代码。

$sql1="SELECT DRCODE FROM DOCTORDETAILS  ORDER BY DRCODEDESC LIMIT 1";
    $query=$this->db->query($sql1);
    if (!$sql1) { // add this check.
        die('Invalid query');
    }
    $output_array2=array();
    foreach($query->result_array() as $row)
    {
        $ids=$row['DRCODE'];
    }
    // echo $ids;
    if($ids){
        $su=1;
        $num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
        $unique=$num;
    }else{
        $unique='DR000001';     
    }

答案 1 :(得分:0)

您应该使用不同的列编码,将其留空。 现在在插入时触发,它应该在插入后运行。

我假设DOCTORDETAILS为表名,drcode为列名

DELIMITER $$

CREATE TRIGGER trigger_after_insert 
AFTER INSERT ON `DOCTORDETAILS` FOR EACH ROW
begin

UPDATE `DOCTORDETAILS` set drcode=CONCAT('DR',new.id);

END$$

DELIMITER ;

触发器使用新的自动生成ID

创建一个新字符串
  

MySQL中的触发器:

     

TechonTheNet

     

MySQL Trigger on after insert

相关问题