【问题标题】:How to Auto Generate a code using Codeigniter framework? example for generate member number for a member card如何使用 Codeigniter 框架自动生成代码?为会员卡生成会员号的示例
【发布时间】:2020-06-23 05:05:20
【问题描述】:

如何为会员编号制作自动生成的代码。我使用 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 功能: 需要一次

遇到 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 功能: 需要一次

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

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

更新customer SET no_member = 'MEM000000000' WHERE customer_id

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

行号:691

【问题讨论】:

  • 你想找回自动生成的Id吗?
  • 您能说得更具体些吗?你能分享一下错误吗?
  • 请在您的问题中直接添加您想要审核的代码,发布图片会使获得概览变得更加困难。如上所述,分享您遇到的错误

标签: php codeigniter auto-generate


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    $insert_id = $this-&gt;db-&gt;insert_id();

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      相关资源
      最近更新 更多