Data insert codeignetter throwing error - codeigniter

PHP Error was encountered Severity: Notice Message: Undefined
property: Registerform::$db Filename: core/Model.php Line Number: 51
controller file:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Registerform::$db
Filename: core/Model.php
Line Number: 51
controller file:
<?php
class Registerform extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Registerform_model');
}
public function index() {
//$this->load->database();
$this->load->helper("form");
$this->load->library("form_validation");
$this->form_validation->set_rules("first", "First Name","required");
$this->form_validation->set_rules("last", "Last Name","required");
$this->form_validation->set_rules("email", "Email Address","required|valid_email");
$this->form_validation->set_rules("password", "Password","required");
$this->form_validation->set_rules("phone", "Phone Number","required");
$this->form_validation->set_rules("city", "City","required");
$this->form_validation->set_rules("addrss", " Your Address","required");
if ($this->form_validation->run() == false) {
$this->load->view("registerform_view");
} else {
$first = $_POST["first"];
$last = $_POST["last"];
$email = $_POST["email"];
$password = $_POST["password"];
$phone = $_POST["phone"];
$city = $_POST["city"];
$addrss = $_POST["addrss"];
$data = array(
'first'=>$first,
'last'=>$last,
'email'=>$email,
'password'=>$password,
'phone'=>$phone,
'city'=>$city,
'addrss'=>$addrss
);
//$data="insert into address (first_name,last_name,email,password,phone,city,add) values('$first','$last','$email','$password','$phone','$city','$add')";
$gg = $this->Registerform_model->insert_address($data);
echo $gg;
$this->load->view("registerformsuccess");
}
}
}
?>
model file:
<?php
class Registerform_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert_address($data){
$this->db->insert("address", $data);
}
}
?>
view file:
<?php
echo validation_errors();
echo form_open("Registerform");
?>
<label for="first">First Name</label>
<input type="text" name="first" id="first"><br>
<label for="last">Last Name</label>
<input type="text" name="last" id="last"><br>
<label for="email">Email Address</label>
<input type="text" name="email" id="email"><br>
<label for="password">Password</label>
<input type="password" name="password" id="password"><br>
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone"><br>
<label for="city">City Name</label>
<input type="text" name="city" id="city"><br>
<label for="addrss">Your Address</label>
<input type="text" name="addrss" id="addrss"><br>
<input type="submit" name="submit" id="submit" value="Submit">
<?php echo form_close(); ?>

<?php
class Registerform_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert_address($data){
$this->load->database();
$this->db->insert("address", $data);
}
}
?>
Its work for me now.

You have to load database.
Put this line in model file constructor function:
$this->load->database();

Related

POST method is not working in Codeigniter v4

I'm using form_open() for open form but form method is always showing get.
echo $this->request->getMethod() showing always get.
generated HTML is:
<form action="http://darkadmin.com/admin" method="post" accept-charset="utf-8">
<input type="hidden" name="_csrf" value="45435e3abd2d067883dacd6f62280fa7" />
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
View:
<?php if(isset($validation)) { ?>
<?php echo $validation->listErrors(); ?>
<?php } ?>
<?php echo form_open(current_url().'admin'); ?>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
<?php echo form_close(); ?>
Controller:
<?php
namespace App\Controllers;
class Login extends BaseController
{
public function __construct(){
helper(['form', 'url']);
}
public function index()
{
$rules = [
'fname' => 'required',
'lname' => 'required'
];
echo $this->request->getMethod();
if($this->request->getMethod() == 'post' && $this->validate($rules)){
echo 'Success';
} else {
$this->data['validation'] = $this->validator;
}
return view($this->data['config']->theme.'login_view',$this->data);
}
}
How can I solve this problem? Please help me

in CodeIgniter form_validation not working

<form action="<?php echo base_url().'login/index'; ?>" method="post" name="loginForm" id="loginForm">
<div class="mb-3">
<input type="text" class="form-control" id="username" name="username" autocomplete="off">
</div>
<div class="mb-3">
<input type="password" class="form-control" id="password" name="password" autocomplete="off">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->load->library('form_validation');
$this->form_validation-
>set_rules('username','username','required');
$this->form_validation-
>set_rules('password','Password','required');
if($this->form_validation->run() == false){
$this->load->view('admin/login');
}
else{
echo"Form validation Successfully";
}
}
}
You are using base_url() without adding this library
// need this to use base_url() in the template.php
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
try with this line again

!empty function wont work on login using codeigniter

This is my login form
<form action="<?php echo site_url('admin/checkAdmin')?>
<div class="form-group">
<input type="text" name="email" placeholder="Enter Your Email" class="form-control">
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Enter Your Password" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Signin</button>
</div>
</form>
And my controller
public function checkAdmin()
{
$data['aEmail'] = $this->input->post('email', true);
$data['aPassword'] = $this->input->post('password', true);
if (!empty($data['aEmail']) && !empty($data['aPassword']))
{
echo 'working';
}
else
{
echo 'not working';
}
}
Whether I key in values or not, it still echos 'not working'. Where is the problem?
Controller Code:-
class admin extends CI_Controller {
function login()
{
$email= $this->input->post("email");
$password = $this->input->post("password");
$result = $this->db->get_where('table_name',array('email'=>$email,'password'=>$password))->row_array();
if ($result >0) //active user record is present
{
$url = base_url('admin/Admin/dashboard');
redirect($url);
}
else{
redirect('admin/Admin');
}
}
}

How to input multiple value with checkbox in CodeIgniter?

I have prepared the form to be inputted to the database, but specifically for multiple checkboxes. I've found a similar case with the solutionbut not with the algorithm that I use
Here it is my controller
class Ruang extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("m_ruang");
$this->load->library('form_validation');
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$data["ruang"] = $this->m_ruang->getAll();
$this->load->view('admin/ruang/index.php', $data);
}
public function add()
{
$ruang = $this->m_ruang;
$validation = $this->form_validation;
$validation->set_rules($ruang->rules());
if ($validation->run()) {
$ruang->save();
$this->session->set_flashdata('success', 'Berhasil ditambahkan');
}
$this->load->view("admin/ruang/add_ruang");
}
Here it is my models
class M_ruang extends CI_Model
{
private $_table = "ruang";
public $id_ruang;
public $ruang;
public $kapasitas_kuliah;
public $kapasitas_ujian;
public $layout;
public $fasilitas;
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_ruang" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_ruang = uniqid();
$this->ruang = $post["ruang"];
$this->kapasitas_kuliah = $post["kapasitas_kuliah"];
$this->kapasitas_ujian = $post["kapasitas_ujian"];
$this->layout = $post["layout"];
$this->fasilitas = $post["fasilitas"];
$this->db->insert($this->_table, $this);
}
and here part of form view
<form action="<?php base_url('ruang/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="ruang">Nama Ruang</label>
<input class="form-control <?php echo form_error('ruang') ? 'is-invalid':'' ?>"
type="text" name="ruang" placeholder="Masukkan nama ruangan" />
<div class="invalid-feedback">
<?php echo form_error('ruang') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_kuliah">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_kuliah') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_kuliah" min="0" placeholder="Tentukan kapasitas kuliah" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_kuliah') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_ujian">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_ujian') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_ujian" min="0" placeholder="Tentukan kapasitas ujian" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_ujian') ?>
</div>
</div>
<div class="form-group">
<label for="layout">Layout</label>
<input class="form-control"
data-inputmask="'mask': ['99 x 99']" data-mask
type="text" name="layout" placeholder="Tentukan layout ruangan" />
</div>
<div class="form-group">
<label for="fasilitas">Fasilitas Tersedia</label> <br>
<input type="checkbox" name="fasilitas[]" value="Proyektor"> Proyektor
<br>
<input type="checkbox" name="fasilitas[]" value="Papan Tulis"> Papan Tulis
<br>
<input type="checkbox" name="fasilitas[]" value="Jam Dinding"> Jam Dinding
<br>
<input type="checkbox" name="fasilitas[]" value="AC"> AC
<br>
<input type="checkbox" name="fasilitas[]" value="Kipas Angin"> Kipas Angin
<br>
<input type="checkbox" name="fasilitas[]" value="Tong Sampah"> Tong Sampah
<div class="invalid-feedback">
<?php echo form_error('fasilitas') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
This really hinders my project, I hope someone can help
You can use the following line too :
$fasilitas = implode(',', $this->input->post( 'fasilitas' , TRUE ) );
If you can save fasilitas in your database as string. Then you can implode fasilitas array with comma separated as shown below:
$this->fasilitas = implode(',',$post["fasilitas"]);
it will stored in back-end side(Database) something like that.
Proyektor,Papan Tulis
I hope this will works for you.
You Can Use This to Get fasilitas as array :
$fasilitas = $this->input->post('fasilitas'); // Like array('AC','Proyektor','Kipas Angin');
In order for you to get all the checked boxes store in database, write this code.
$values = $post['fasilitas'];
$fasilitas = "";
foreach($values as $val)
{
$fasilitas .= $val . ", ";
}
Then store $fasilitas to db.
$data = array(
'fasilitas' => $fasilitas,
);
$this->db->insert('table_name', $data);
Hope that helps :)

codeigniter submits to unknown form

I have this form:
<html>
<head>
<title>التسجيل</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body dir="rtl">
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>الإسم الكامل:</h5>
<input type="text" name="fullname" value="" size="50" />
<h5>الجوال:</h5>
<input type="text" name="mobile" value="" size="50" />
<h5>هاتف المنزل:</h5>
<input type="text" name="home" value="" size="50" />
<h5>اسم المستخدم:</h5>
<input type="text" name="username" value="" size="50" />
<h5>كلمة السر:</h5>
<input type="text" name="password" value="" size="50" />
<h5>اعادة كلمة السر:</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>الايميل:</h5>
<input type="text" name="email" value="" size="50" />
<br><br>
<div><input type="submit" value="التسجيل" /></div>
</form>
</body>
</html>
Please don't mind the arabic writings.The controller as follows:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Membership extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
public function index()
{
//Loads the login page which has a registration option
$this->load->view('login');
}
public function register()
{
$post = $this->input->post();
if($post)
{
//handle posted data
$fullname = $this->input->post('fullname');
$email = $this->input->post('email');
$mobile = $this->input->post('mobile');
$home = $this->input->post('home');
$username = $this->input->post('username');
$password = $this->input->post('password');
$confirmPassword = $this->input->post('cpassword');
$memberType = 'silver';
if ($this->form_validation->run() == FALSE)
{
$this->load->view('register');
}
else
{
$this->load->view('welcome_message');
}
}else{
$this->load->view('register');
}
}
}
Now when I click submit button on the form, it shows 404 and link in address bar is:
http://domain.com/index.php/form
Why is this happening? It should check if posted, then load x. if not load y.
Am a beginner in codeigniter so apologies.
As you have copied code from CI user guide :
form_open('form'); // here is a form action which is form class index function
just change it to :
form_open('membership/register');
which means :
form_open('location/to/sumbit/to');
that's it!
Change
<?php echo form_open('form'); ?>
To
<?php echo form_open('membership/register'); ?> // class/function

Resources