I’m so tired of thinking how to solve this case. already search by internet and never solved. straight to the point
this is my Controller of Product.php
<?php namespace AppControllers; use CodeIgniterController; use AppModelsModel_product; use AppModelsModel_category; use AppModelsModel_status; use AppModelsModel_supplier; class Product extends Controller { protected $helpers = []; public function __construct() { helper(['form']); $this->model_category = new Model_category(); $this->model_product = new Model_product(); $this->model_status = new Model_status(); $this->model_supplier = new Model_supplier(); } public function index() { $data['tbproduct'] = $this->model_product->getProduct(); return view('product/index', $data); } public function create() { $all = [ 'tbproduct' => $this->model_product->getProduct(), 'tbcategory' => $this->model_category->getCategory(), 'tbstatus' => $this->model_status->getStatus(), 'tbsupplier' => $this->model_supplier->getSupplier() ]; return view('product/create', $all); } public function store() { $validation = ConfigServices::validation(); $data = array( 'prod_name' => $this->request->getPost('prod_name'), 'prodcat_id' => $this->request->getGet('cat_id'), 'prod_serial' => $this->request->getPost('prod_serial'), 'prod_barcode' => $this->request->getPost('prod_barcode'), 'prodstat_id' => $this->request->getPost('stat_id'), 'prodsup_id' => $this->request->getPost('sup_id'), 'prod_image' => $this->request->getPost('prod_image') ); if ($validation->run($data, 'product') == FALSE) { session()->setFlashdata('inputs', $this->request->getPost()); session()->setFlashdata('errors', $validation->getErrors()); return redirect()->to(base_url('product/create')); } else { $model = new Model_product(); $simpan = $model->insertProduct($data); if ($simpan) { session()->setFlashdata('Success', 'Product has been created'); return redirect()->to(base_url('product')); } } } }
this is my Models Model_product.php
<?php namespace AppModels; use CodeIgniterModel; class Model_product extends Model { protected $table = 'tbproduct'; public function getProduct($id = false) { if ($id === false) { return $this->table('tbproducts') ->join('tbcategory', 'tbproduct.prodcat_id = tbcategory.cat_id', 'left') ->join('tbstatus', 'tbproduct.prodstat_id = tbstatus.stat_id', 'left') ->join('tbsupplier', 'tbproduct.prodsup_id = tbsupplier.sup_id', 'left') ->get() ->getResultArray(); } else { return $this->table('tbproducts') ->join('tbcategory', 'tbproduct.prodcat_id = tbcategory.cat_id', 'left') ->join('tbstatus', 'tbproduct.prodstat_id = tbstatus.stat_id', 'left') ->join('tbsupplier', 'tbproduct.prodsup_id = tbsupplier.sup_id', 'left') ->where('tbproduct.prod_id', $id) ->get() ->getRowArray(); } } public function insertProduct($data) { return $this->db->table($this->table)->insert($data); } public function updateProduct($data, $id) { return $this->db->table($this->table)->update($data, ['prod_id' => $id]); } public function deleteProduct($id) { return $this->db->table($this->table)->delete(['prod_id' => $id]); } }
and this is my Views create.php
<?php echo view('_partials/header'); ?> <?php echo view('_partials/sidebar'); ?> <div class="content-wrapper"> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">Create New Product</h1> </div> <div class="col-sm-6"> <ol class="breadcrumb float-sm-right"> <li class="breadcrumb-item"><a href="/">Home</a></li> <li class="breadcrumb-item active">Create New Product</li> </ol> </div> </div> </div> </div> <div class="content"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <form action="<?php echo base_url('product/store'); ?>" method="post"> <div class="card"> <div class="card-body"> <?php $inputs = session()->getFlashdata('inputs'); //$inputs_cat = isset($inputs['cat_id']) == null ? '' : $inputs['cat_id']; $errors = session()->getFlashdata('errors'); if (!empty($errors)) { ?> <div class="alert alert-danger" role="alert"> Whoops! There is something wrong, that is: <ul> <?php foreach ($errors as $error) : ?> <li><?= esc($error) ?></li> <?php endforeach ?> </ul> </div> <?php } ?> <div class="form-group"> <label for="">Product Name</label> <input type="text" class="form-control" name="prod_name" placeholder="Enter product name" value="<?php echo isset($inputs['prod_name']); ?>"> </div> <div class="form-row"> <div class="col mb-3"> <label for="">Category</label> <select class="form-control" name="cat_name" id="cat_name"> <?php foreach ($tbcategory as $row) { echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>'; } ?> </select> </div> <div class="col mb-3"> <label for="">Serial Number</label> <input type="text" class="form-control" name="prod_serial" placeholder="Enter product serial number" value="<?php echo isset($inputs['prod_serial']); ?>"> </div> <div class="col mb-3"> <label for="">Barcode</label> <input type="text" class="form-control" name="prod_barcode" placeholder="Enter product barcode" value="<?php echo isset($inputs['prod_barcode']); ?>"> </div> </div> <div class="form-row"> <div class="col mb-3"> <label for="">Status</label> <select class="form-control" name="stat_name" id="stat_name"> <?php foreach ($tbstatus as $row) { echo '<option value="' . $row['stat_id'] . '">' . $row['stat_name'] . '</option>'; } ?> </select> </div> <div class="col mb-3"> <label for="">Supplier</label> <select class="form-control" name="sup_name" id="sup_name"> <?php foreach ($tbsupplier as $row) { echo '<option value="' . $row['sup_id'] . '">' . $row['sup_name'] . '</option>'; } ?> </select> </div> </div> </div> <div class="card-footer"> <a href="<?php echo base_url('product'); ?>" class="btn btn-outline-info">Back</a> <button type="submit" class="btn btn-primary float-right">Save</button> </div> </div> </form> </div> </div> </div> </div> </div> <?php echo view('_partials/footer'); ?>
i want to insert new data from Controller Product function store. i’ve this problem, i cannot change string value into integer value for this
$data = array( 'prod_name' => $this->request->getPost('prod_name'), 'prodcat_id' => $this->request->getGet('cat_id'), ---->>> this is the problem 'prod_serial' => $this->request->getPost('prod_serial'), 'prod_barcode' => $this->request->getPost('prod_barcode'), 'prodstat_id' => $this->request->getPost('stat_id'), ---->>> and this 'prodsup_id' => $this->request->getPost('sup_id'), ---->>> and also this 'prod_image' => $this->request->getPost('prod_image') );
if you see in the create.php Views, there is $row[‘***_id’] for all three tables. and i want to grab that three ids into store.
and this is the Error shown:
mysqli_sql_exception #1048 Column 'prodcat_id' cannot be null
Advertisement
Answer
thank you for helping me before. i found the answer. here is the correct code where i can get the cat_id, stat_id and sup_id.
in the View create.php change this code
<select class="form-control" name="cat_name" id="cat_name">
into this
<select class="form-control" name="cat_id" id="cat_id">
this mean you will get the cat_id value from the database, instead of cat_name. change the stat_name into stat_id, then sup_name into sup_id.
when all of that changed, you can insert the database with the correct id number when you selected the value from the dropdown box.
this is the answer of my problem, thank you for helping me.