繁体   English   中英

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

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

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

这是我的代码:

//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>';
    }
}

错误如下:

遇到 PHP 错误严重性:4096

消息:类 CI_DB_mysqli_result 的对象无法转换为字符串

文件名:admin/Customer.php

行号:46

回溯:

文件:C:\\xampp\\htdocs\\tokobuku\\application\\controllers\\admin\\Customer.php 行:46 功能:sprintf

文件:C:\\xampp\\htdocs\\tokobuku\\index.php 行:315 功能:require_once

遇到 PHP 错误严重性:4096

消息:类 CI_DB_mysqli_result 的对象无法转换为字符串

文件名:数据库/DB_query_builder.php

行号:2442

回溯:

文件:C:\\xampp\\htdocs\\tokobuku\\application\\models\\Customer_model.php 行:32 功能:更新

文件:C:\\xampp\\htdocs\\tokobuku\\application\\controllers\\admin\\Customer.php 行:51 功能:customer_update

文件:C:\\xampp\\htdocs\\tokobuku\\index.php 行:315 功能:require_once

发生数据库错误错误号:1064

您的 SQL 语法有错误; 检查与您的 MariaDB 服务器版本相对应的手册,以了解在第 2 行的 '' 附近使用的正确语法

更新customer SET no_member = 'MEM000000000' WHERE customer_id

文件名:C:/xampp/htdocs/tokobuku/system/database/DB_driver.php

行号:691

通过模型,您似乎想要检索最后插入的客户 ID。如果是这样,您可以在 codeigniter 中使用它,就在您执行插入客户查询之后:

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

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

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

在您的模型中更改此代码然后尝试

 //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);    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM