Welcome Guest, Not a member yet? Register   Sign In
Why unknow column 'slug_safelink' ?
#1
Question 
(This post was last modified: 11-05-2020, 12:15 AM by flux1on.)

I have problem Unknown column 'slug_safelink' in 'where clause'

Controller :
Code:
<?php namespace App\Controllers;
use App\Models\Safelink_model;

class Safelink extends BaseController
{
    public function index()
    {
        /**
        * Load Module
        */
        $Safelink_model = new Safelink_model();

        $data['data'] = $Safelink_model->getData();
        $data['config'] = $this->settings;
        $data['recaptchaWidget'] = $this->recaptcha->getWidget();
        $data['recaptchaScript'] = $this->recaptcha->getScriptTag();
        $data['pesan_gagal'] = $this->session->getFlashdata('inputs');
        $data['error_gagal'] = $this->session->getFlashdata('errors');
        $data['pesan_berhasil'] = $this->session->getFlashdata('success');

        $data['errlog'] = $this->session->getFlashdata('errlog');

        echo view('link/header', $data);
        echo view('link/home', $data);
        echo view('link/footer', $data);
    }

    // Save Data
    public function simpan()
    {
        /**
        * Load Module
        */
        $Safelink_model = new Safelink_model();

        $namaP = esc($this->request->getPost('nama'));
        $slugP = esc($this->request->getPost('slug'));
        $urlP = esc($this->request->getPost('url'));
        $captcha = $this->request->getPost('g-recaptcha-response');

        $validationForm = [
            'nama' => $namaP,
            'slug' => $slugP,
            'url' => $urlP
        ];

        if($this->form_validation->run($validationForm, 'safelink') == FALSE){
            // mengembalikan nilai input yang sudah dimasukan sebelumnya
            $this->session->setFlashdata('inputs', $this->request->getPost());
            // memberikan pesan error pada saat input data
            $this->session->setFlashdata('errors', $this->form_validation->getErrors());
            // kembali ke halaman form
            return redirect()->to(base_url('link'));
        } else {
            $response = $this->recaptcha->verifyResponse($captcha);
            if (isset($response['success']) AND $response['success'] === true) {
                /**
                * Success Verify Anti Bot
                */
                // Apa yang mau disimpan
                $data = [
                    'kode_safelink' => NULL,
                       'nama_safelink' => $namaP,
                       'slug_safelink' => $slugP,
                    'url_safelink' => $urlP,
                    'hits_safelink' => NULL,
                    'created_safelink' => now()
                ];

                $simpan = $Safelink_model->saveData($data);
                    
                if ($simpan) {
                    $this->session->setFlashdata('success', 'Simpan Data Berhasil!');
                    // kembali ke halaman utama
                    return redirect()->to(base_url('link'));
                } else {
                    // memberikan pesan error pada saat input data
                    $this->session->setFlashdata('errors', 'Simpan Data Gagal!');
                    // kembali ke halaman form
                    return redirect()->to(base_url('link'));
                }
            } else {
                // memberikan pesan error pada saat input data
                $this->session->setFlashdata('errors', 'Captcha gagal!');
                // memberikan data input
                $this->session->setFlashdata('inputs', $this->request->getPost());
                // kembali ke halaman form
                return redirect()->to(base_url('link'));
            }
        }
    }
}


Models :
Code:
<?php namespace App\Models;
use CodeIgniter\Model;

class Safelink_model extends Model
{
    protected $table = 'safelink';
    protected $primaryKey = 'kode_safelink';
    protected $allowedFields = ['kode_safelink', 'nama_safelink', 'slug_safelink', 'url_safelink', 'hits_safelink', 'created_safelink'];

    public function goLink($slug)
    {
        $query = $this->db->table($this->table)
                        ->where('slug_safelink', $slug)
                        ->get();

        return $query->getRow();
    }
    
    public function getData($id = null)
    {
        if($id === null){
            return $this->findAll();
        } else {
            return $this->getWhere(['kode_safelink' => $id]);
        }  
    }

    public function saveData($data)
    {
        $query = $this->db->table($this->table)->insert($data);
        return $query;
    }

    public function updateData($data, $id)
    {
        $query = $this->db->table($this->table)->update($data, array('kode_safelink' => $id));
        return $query;
    }

    public function CounterSafelink($data, $id)
    {
        $query = $this->db->table($this->table)->update(array('hits_safelink' => $data+1), array('slug_safelink' => $id));
        return $query;
    }

    public function deleteData($id)
    {
        $query = $this->db->table($this->table)->delete(array('kode_safelink' => $id));
        return $query;
    }
}


Structure Table :
Code:
CREATE TABLE `safelink` (
  `kode_safelink` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama_safelink` varchar(80) NOT NULL,
  `slug_safelink` text NOT NULL UNIQUE,
  `url_safelink` text NOT NULL,
  `hits_safelink` bigint NOT NULL DEFAULT '0',
  `created_safelink` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Attached Files Thumbnail(s)
   
Reply
#2

Did you initialize the $allowedFields array in your Model?

PHP Code:
protected $allowedFields = ['nama_safelink''slug_safelink''url_safelink''hits_safelink']; 

Also, you don't really need the saveData, updateDate, and deleteData methods if you are using CodeIgniter 4's model.  CodeIgniter already has insert, update, delete, and save methods available to you: https://codeigniter.com/user_guide/models/model.html.
Reply
#3

(11-04-2020, 10:45 AM)mlurie Wrote: Did you initialize the $allowedFields array in your Model?

PHP Code:
protected $allowedFields = ['nama_safelink''slug_safelink''url_safelink''hits_safelink']; 

Also, you don't really need the saveData, updateDate, and deleteData methods if you are using CodeIgniter 4's model.  CodeIgniter already has insert, update, delete, and save methods available to you: https://codeigniter.com/user_guide/models/model.html.

i already add $allowedfields to all models, sry no update in thread

Code:
<?php namespace App\Models;
use CodeIgniter\Model;

class Safelink_model extends Model
{
    protected $table = 'safelink';
    protected $primaryKey = 'kode_safelink';
    protected $allowedFields = ['kode_safelink', 'nama_safelink', 'slug_safelink', 'url_safelink', 'hits_safelink', 'created_safelink'];

    public function goLink($slug)
    {
        $query = $this->db->table($this->table)
                        ->where('slug_safelink', $slug)
                        ->get();

        return $query->getRow();
    }
     
    public function getData($id = null)
    {
        if($id === null){
            return $this->findAll();
        } else {
            return $this->getWhere(['kode_safelink' => $id]);
        }   
    }

    public function saveData($data)
    {
        $query = $this->db->table($this->table)->insert($data);
        return $query;
    }

    public function updateData($data, $id)
    {
        $query = $this->db->table($this->table)->update($data, array('kode_safelink' => $id));
        return $query;
    }

    public function CounterSafelink($data, $id)
    {
        $query = $this->db->table($this->table)->update(array('hits_safelink' => $data+1), array('slug_safelink' => $id));
        return $query;
    }

    public function deleteData($id)
    {
        $query = $this->db->table($this->table)->delete(array('kode_safelink' => $id));
        return $query;
    }
}

i create function saveData,updateData,deleteData for calling function models in controller
Reply
#4

Make sure you spelled 'slug_safelink' the same way in your MySQL database. I just ran into this today.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB