How to make an auto-generated code for member number. I use Codeigniter framework. but it shows some error and all data success entry to the database except “member number” that highlights in error pictured below.
This is my codes:
//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>';
}
}
Error Like Following:
A PHP Error was encountered Severity: 4096
Message: Object of class CI_DB_mysqli_result could not be converted to
stringFilename: admin/Customer.php
Line Number: 46
Backtrace:
File:
C:xampphtdocstokobukuapplicationcontrollersadminCustomer.php
Line: 46 Function: sprintfFile: C:xampphtdocstokobukuindex.php Line: 315 Function:
require_onceA PHP Error was encountered Severity: 4096
Message: Object of class CI_DB_mysqli_result could not be converted to
a stringFilename: database/DB_query_builder.php
Line Number: 2442
Backtrace:
File: C:xampphtdocstokobukuapplicationmodelsCustomer_model.php
Line: 32 Function: updateFile:
C:xampphtdocstokobukuapplicationcontrollersadminCustomer.php
Line: 51 Function: customer_updateFile: C:xampphtdocstokobukuindex.php Line: 315 Function:
require_onceA 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 2UPDATE
customer
SETno_member
= ‘MEM000000000’ WHEREcustomer_id
Filename: C:/xampp/htdocs/tokobuku/system/database/DB_driver.php
Line Number: 691
2
Answers
By the model, it seems that you want to retrieve the last inserted customer Id.If so, you can use this in codeigniter, just after you execute your insert customer query:
$insert_id = $this->db->insert_id();
You should use transactions in case of multiple inserts:
$this->db->trans_start();
$this->db->trans_complete();