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 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
SETno_member
= 'MEM000000000' WHEREcustomer_id
Filename: C:/xampp/htdocs/tokobuku/system/database/DB_driver.php
Line Number: 691
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);
}