Im trying add data to the database useing CodeIgniter framework. DB method return true, but I have no new data in database.
There is my controller:
class Facebook extends CI_Controller
{
public function index()
{
if($this->input->is_ajax_request())
{
$this->load->model('user_model','user');
$data = array();
$data['u_ip'] = $this->input->ip_address();
$this->user->add_user($data);
}
}
}
And there is my model:
class User_model extends CI_Model
{
public function add_user($data)
{
$this->db->insert('users',$data);
//$res = $this->db->insert('users',$data); (true)
}
}
Update:
There is my database:
id_users (int 255) AI, key
u_ip (varchar 100)
Also, make sure your $data looks like this:
$data = array(
"u_ip" => $this->input->ip_address(),
);
$this->insert("data", $data);
Or if coming from another file, do this:
$this->insert("users", $data[0];)
Remember to load the dataabse:
$this->load->database();
This will go within add_user($data) eg.
class User_model extends CI_Model
{
public function add_user($data)
{
$this->load->database();
$this->db->insert('users',$data);
//$res = $this->db->insert('users',$data); (true)
}
}
However, you can have these automatically, if you go to autoload.php, you can do this:
$autoload['libraries'] = array('database');
And for your model:
$autoload['model'] = array('logic');
You are not mentioning any column names in your query. Try the below code -
class User_model extends CI_Model{
public $db;
public function __construct(){
parent::__construct();
$this->db = $this->load->database('default',true);
}
public function add($data)
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
$this->input->is_ajax_request()
is this posted via a form ? if so it should be $this->input->post('is_ajax_request');
is_ajax_request being the name of the input your posting,
If its another function within the controller you need to do $this->is_ajax_request();
Related
I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}
model
This is my model.using last query I get value in the model,but I don't get the value in the controller
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function email()
{
$this->db->select('email');
$this->db->from('change_password');
$result=$this->db->get();
return $result;
}
}
controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();
echo $dbemail;
}
}
CI is a MVC Framework. So you need to Command from Controller and get data from the models and finely you need to pass them to view. This is a best Practice
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
View
Extra Knowledge
View will be create under View folder/ Ex im using view name
as home.php
you can use your own stylings(its also created as normal html page.)
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
I get the error :
Fatal error: Call to undefined method select::form() in public_html/opunletter/application/controllers/Welcome.php on line 44.
when i run this. what might be the reason.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
//load the database
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
$data['a']=$this->select->archieve();
$data['n']=$this->select->latest();
$id=$this->input->get("id");
$data['f']=$this->select->load($id);
//return the data in view
$this->load->view('welcome_message', $data);
}
public function login()
{
$this->load->view('userlogin');
}
public function submit()
{
$data = array(
'from' => $this->input->post('from'),
'to' => $this->input->post('to'),
'title' => $this->input->post('title'),
'openletter' => $this->input->post('openletter')
);
$this->load->model('select');
//Transfering data to Model
$this->select->form($data);
}
} ?>
My model
<?php
class Select extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//we will use the select function
public function Select()
{
//data is retrive from this query
$query = $this->db->get('country');
return $query;
}
public function archieve()
{
//data is retrive from this query
$query = $this->db->query("SELECT * FROM country WHERE archieve=1;");
return $query;
}
public function latest()
{
//data is retrive from this query
$query = $this->db->query("SELECT * FROM country WHERE latest=1;");
return $query;
}
public function load($id)
{
//data is retrive from this query
$query = $this->db->select('*')
->from('country')
->where('open_id', $id)
->get();
return $query;
}
public function form($data){
// Inserting in Table(students) of Database(college)
$query = $this->db->insert('user_openletter', $data);
return $query;
}
}
?>
This is a class for handling model in Codeigniter app
class MY_Model extends CI_Model {
const DB_TABLE = 'abstract';
const DB_TABLE_PK = 'abstract';
private function update() {
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
}
public function save() {
if (isset($this->{$this::DB_TABLE_PK})) {
$this->update();
}
else {
$this->insert();
}}
And this is a model extended from above class:
class Projects extends MY_Model {
const DB_TABLE = 'projects';
const DB_TABLE_PK = 'project_id';
public $project_id;
public $project_name;
public $project_investment;
public $project_employment;
public $project_province;
public $project_city;
public $project_address;
public $project_estimate;
public $project_duration;
public $project_construction;
}
According to Codeigniter User Guide, i think there is a problem in 3rd parameter of Update query (It just send DB_TABLE_PK name ,in this case 'project_id' ) but since i'm new to OOP , don't know how to fix it .
Codeigniter User Guide :
$this->db->update('mytable', $data, "id = 4");
The function above doe not seem to have the complete WHERE statement for the update()
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
will translate to
UPDATE $this::DB_TABLE SET $this WHERE $this::DB_TABLE_PK
and in your example above $this::DB_TABLE_PK is the name of the key ('project_id'), and not its value.
I would stick with
$this->db->where($this::DB_TABLE_PK, $pk)->update($this::DB_TABLE, $this);
Where $pk is the actual value of your PRIMARY KEY
guide clrearly says
$data_array = array();
$where_array = array();
$this->db->update('table_name', $where_array, $data_array());
so example
$data_array = array('name' => 'Alpha', 'gender' => 'male'); // change column name to "Alpha", and gender column to 'male'
$where_array = array('id' => '4', 'allowed' => '1'); // update row with $data_array where id = 4 and allowed = 1
$this->db->update('person', $where_array, $data_array());
I strongly recommend you to use profiler $this->output->enable_profiler(TRUE); put this anywhere in controller and under your page there will be "profiler" is shows post/get data queries, sessions and all usefull information while developing.
i fixed it :
private function update() {
$value=$this::DB_TABLE_PK;
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK.' = '.$this->$value);
}
From a user login form i need to use their username ($this->input->post('username')) to query against the membership table in my database so that I can retrieve the users firstname and lastname, which I believe should be done via a model called from my controller. I then need to pass that data back to my controller to use the firstname and lastname to add in to my session userdata which is set in my controller, as shown below.
$data = array(
'username' => $this->input->post('username'),
'firstname' => //need to retrieve value from database,
'lastname' => //need to retrieve value from database,
'is_logged_in' => true
);
$this->session->set_userdata($data);
Thanks,
Kristan.
In your model, make a function to retrieve the first and last name of a user:
public function get_user_details($username){
// retrieve contents from db:
// the first username here is the name of the column in your table
$query = $this->db->select('firstname, lastname')
->where('username', $username)
->get('membership');
return $query->result_array();
}
In your controller, like you were doijng, grab the POST contents in your controller:
$username = $this->input->post('username');
Then, from your controller, call your model function but save the output of that model function in a variable:
$data = $this->user_model->get_user_details($username);
After that, you can do what you were trying to do:
$this->session->set_userdata($data);
Hope that helps. I wrote this from the top of my head but it should work. I also suggest you read the CI documentation through because it really is some of the best documentation I have ever seen for a framework. Good luck.
Please check CodeIgniter user guide for model. The example of Blog controller and Blog model will answer you.
class Blogmodel extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
function insert_entry()
{
$this->title = $_POST['title']; // please read the below note
$this->content = $_POST['content'];
$this->date = time();
$this->db->insert('entries', $this);
}
function update_entry()
{
$this->title = $_POST['title'];
$this->content = $_POST['content'];
$this->date = time();
$this->db->update('entries', $this, array('id' => $_POST['id']));
}
}
class Blog_controller extends CI_Controller {
function blog()
{
$this->load->model('Blog');
$data['query'] = $this->Blog->get_last_ten_entries();
$this->load->view('blog', $data);
}
}
You first need to create a model.
class Membership_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getUser($username) {
$query = $this->db->get_where('membership', array('username' => $username));
return $query->result_array();
}
}
then in your controller, use the membership model to retrieve data. Calling this function: $this->membership_model->getUser($this->input->post('username'));