|
 |
|
สอบถาม Insert ข้อมูลไม่พบ Error แต่ไม่ข้อมูลไม่ยอมเข้า Database ครับ (Codeigniter) |
|
 |
|
|
 |
 |
|
Controller addProduct
public function addProduct()
{
//$data['permission'] = $this->checkPermission();
$data['saveproduct'] = $this->saveProduct();
$data['showproduct'] = $this->Insert_model->get_product();
$data['getcategory'] = $this->Insert_model->get_category();
$data['header1'] = "backend/srcback/Header_v1";
$data['sidebar1'] = "backend/srcback/Sidebar_v1";
$data['content1'] = "backend/srcback/product/add";
$data['content2'] = "backend/srcback/product/productlist";
$data['settingbar1'] = "backend/srcback/Settingbar_v1";
$this->load->view('backend/template/dashboard', $data);
}
Controller saveProduct
public function saveProduct()
{
$this->load->library('form_validation');
$input = $this->input->post();
if(!empty($input)){
$this->form_validation->set_rules('name_product', '<b>โปรดกรอกชื่อสินค้า</b>', 'required');
$this->form_validation->set_rules('part_number', '<b>โปรดกรอกเลข P/N</b>', 'required');
$this->form_validation->set_rules('main_price', '<b>โปรดกรอกราคา Main Price</b>', 'required');
$this->form_validation->set_rules('volume_price', '<b>โปรดกรอกราคา Volume Price</b>', 'required');
$this->form_validation->set_rules('description', '<b>โปรดกรอกรายละเอียด</b>', 'required');
if($this->form_validation->run()){
if(empty($input['pro_id'])){
$this->Insert_model->create($input);
}else{
$this->Insert_model->editProduct($input['pro_id'],$input);
}
redirect(base_url('Backend/addProduct'));
}
}
Model - Insert_model
public function create($newdata)
{
$config['upload_path'] = './application/upload/product/'; // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค
$config['allowed_types'] = 'gif|jpg|png'; // ปรเเภทไฟล์
$config['max_size'] = '0'; // ขนาดไฟล์ (kb) 0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB
$config['max_width'] = '1200'; // ความกว้างรูปไม่เกิน
$config['max_height'] = '1024'; // ความสูงรูปไม่เกิน
$config['file_name'] = ''; // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม
$this->upload->initialize($config); // เรียกใช้การตั้งค่า
$this->upload->do_upload('file_img'); // ทำการอัพโหลดไฟล์จาก input file ชื่อ file_img
$file_upload=$this->upload->data('file_name'); // กำหนดชื่อไฟล์เป็นค่าว่าง
if($this->upload->display_errors()){ // ถ้ามี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล
redirect('Backend/addProduct');
}else{
$newdata = array(
'product_id' => NULL,
'product_name' => $this->input->post('name_product'),
'product_pn' => $this->input->post('part_number'),
'product_mprice' => $this->input->post('main_price'),
'product_vprice' => $this->input->post('volume_price'),
'product_stock' => $this->input->post('inout_stock'),
'product_detail' => $this->input->post('description'),
'product_img' => $file_upload,
'product_category' => $this->input->post('product_category')
);
return $this->db
->set('created_at', 'NOW()', FALSE)
->insert($this->tableproduct, $newdata);
}
}
Tag : PHP, MySQL, CodeIgniter

|
|
 |
 |
 |
 |
Date :
2018-08-27 13:49:43 |
By :
oyzceep |
View :
988 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มี Error ก็ลอง Dubug ดูครับ
1.ลองไล่ echo ค่าพวกตัวแปรที่ถูกส่งมาแต่ละจุดดูครับ ดูขั้นตอนว่ามันควรจะมีอะไรออกมา แต่ตอน echo โปรแกรมจะทำงานไม่เสร็จ
นะครับ มันจะค้างตรง echo ล่ะ ถ้ามีอะไรมาจริง ปิด echo จุดนั้น แล้ว echo จุดต่อไป
2. ถ้าโปรแกรมมันทำงานไม่จบ ลองดูใน chrome ครับ กด F12 รีเฟซเพจ ไล่ดูข้อมูลที่ส่งกลับมา ทำทีละขึ้นตอน จะต้องเห็นความผิดปกติสักอย่าง
ผมเกิดปัญหาที่ไร ผมใช้ 2 วิธีนี้ มันจะช้าแต่มันจะรับรู้ได้ด้วยตัวเองว่าโปรแกรมมันทำงานไปทางไหน
ลองทำดูครับยังไงมันต้องเจอว่าปัญหาเกิดจากอะไรครับ
อีกอย่างปิด redirect ไปก่อนครับลองทดสอบงานให้จบ แล้วจะส่งไปหน้าไหนก็ว่ากัน...
ตัวอย่างที่ผม debug ใน models งานจบก็คอมเม้นท์ไว้
public function insert_driver_name() //บันทึกหมวดหมู่หลัก
{
//$title = $this->input->post('title');
//print_r($_POST); เอาให้ชัวร์ว่ามีค่า post ส่งมาแน่
$data = array(
'name' => $this->input->post('name'),
'positon' => 'y'
);
$this->db->insert('driver_name', $data);
return $this->db->affected_rows();
}
public function code_search($start, $end, $id)
{
// echo $start."<hr>";
// echo $end."<hr>";
// echo $id."<hr>";
$this->db->select('*');
$this->db->from('code_data');
$this->db->where("(`use_date` BETWEEN '".$start."' AND '".$end."') AND `ref_nameid`= ".$id."");
$query = $this->db->get();
$search = $query->result();
return $search;
}
|
ประวัติการแก้ไข 2018-08-27 16:27:49
 |
 |
 |
 |
Date :
2018-08-27 15:27:38 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ เด่วจะลองทดสอบดูครับ 
|
 |
 |
 |
 |
Date :
2018-08-27 15:42:05 |
By :
oyzceep |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|