如何使用 Codeigniter 框架自动生成代码?为会员卡生成会员编号的例子

How to Auto Generate a code using Codeigniter framework? example for generate member number for a member card

如何为会员编号制作自动生成代码。我使用 Codeigniter 框架。但它显示了一些错误和所有数据成功进入数据库,除了 "member number" 在下图错误中突出显示。

这是我的代码:

//Model
public function customer_insert($table, $data)
{
    $query = $this->db->insert($table, $data);
    return $query;
}
public function customer_getByLastId()
{
    $query = $this->db->query("SELECT customer_id FROM customer where customer_id=last_insert_id()");
    return $query;
}
public function customer_update($id, $table, $data)
{
    $query = $this->db->where('customer_id', $id);
    $query = $this->db->update($table, $data);
    return $query;
}

//Controller
public function add()
{

    $name = strip_tags($this->input->post('i_name'));
    $ktp = strip_tags($this->input->post('i_ktp'));
    $email = strip_tags($this->input->post('i_email'));
    $gender = strip_tags($this->input->post('i_gender'));
    $phone = strip_tags($this->input->post('i_phone'));
    $address = strip_tags($this->input->post('i_address'));

    // Input Array
    $data = array(
        'name'             => $name,
        'ktp'             => $ktp,
        'email'             => $email,
        'gender'             => $gender,
        'phone'             => $phone,
        'address'             => $address
    );

    // Insert ke Database
    $x = $this->customer_model->customer_cek($ktp);

    if ($x == Null) {
        $this->customer_model->customer_insert('customer', $data);

        $id = $this->customer_model->customer_getByLastId();
        $char = "MEM";
        $no_member = $char . sprintf("%09s", $id);

        $data = array(
            'no_member'             => $no_member
        );
        $this->customer_model->customer_update($id, 'customer', $data);

        echo '<script language=JavaScript>alert("Input Berhasil")
            onclick=history.go(-0);</script>';
    } else {
        echo '<script language=JavaScript>alert("Gagal!! customer telah tersimpan sebelumnya karena Nama atau dan No. Hp sama!")
            onclick=history.go(-1);</script>';
    }
}

如下错误:

A PHP Error was encountered Severity: 4096

Message: Object of class CI_DB_mysqli_result could not be converted to string

Filename: admin/Customer.php

Line Number: 46

Backtrace:

File: C:\xampp\htdocs\tokobuku\application\controllers\admin\Customer.php Line: 46 Function: sprintf

File: C:\xampp\htdocs\tokobuku\index.php Line: 315 Function: require_once

A PHP Error was encountered Severity: 4096

Message: Object of class CI_DB_mysqli_result could not be converted to a string

Filename: database/DB_query_builder.php

Line Number: 2442

Backtrace:

File: C:\xampp\htdocs\tokobuku\application\models\Customer_model.php Line: 32 Function: update

File: C:\xampp\htdocs\tokobuku\application\controllers\admin\Customer.php Line: 51 Function: customer_update

File: C:\xampp\htdocs\tokobuku\index.php Line: 315 Function: require_once

A Database Error Occurred Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2

UPDATE customer SET no_member = 'MEM000000000' WHERE customer_id

Filename: C:/xampp/htdocs/tokobuku/system/database/DB_driver.php

Line Number: 691

根据模型,您似乎想要检索最后插入的客户 Id.If 因此,您可以在执行插入客户查询后在 codeigniter 中使用它:

$insert_id = $this->db->insert_id();

您应该在多次插入的情况下使用事务:

$this->db->trans_start(); $this->db->trans_complete();

Change this codes in your model then try

 //Model
public function customer_insert($table, $data){
   return $this->db->insert($table, $data);
}
public function customer_getByLastId(){
    return  $this->db->query("SELECT customer_id FROM customer ORDER BY customer_id DESC LIMIT 1 ")->row()->customer_id;    
}
public function customer_update($id, $table, $data){ 
    $this->db->where('customer_id', $id);
     return $this->db->update($table, $data);    
}