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;
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;
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.
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;