CI Database Error - php

I'm freshman in CI.
Today I got an error like this when I learned CI tutorial.
You must use the "set" method to update an entry.
Filename:
F:\xampp\htdocs\ISA2013\ISA2013\system\database\DB_active_rec.php
Line Number: 1174
My Model code like this.
class Apply_model extends CI_Model {
public function add_record($data){
$query = $this->db->insert('help_user');
//return;
}
}
And I just coded like that in Control:
public function create(){
$data = array('USER_NUMBER' => $this->input->post('stuNO'),
'USER_EMAIL' => $this->input->post('email'),
'USER_INFO' => $this->input->post('info')
);
$this->apply_model->add_record($data);
$this->index();
}
..when I run class/create, I got the top of error..
Can someone help me?

You need to pass your data as parameter to $this->db->insert function as mentioned by nevermind

As mentioned by nevermind, you have to pass data array to the insert function. Also please note that data array should be an associative array, where keys will be the table fields and values will be values for the fields.

You don't pass the parameter data into $this->db->insert function. You can change the code line :
$query = $this->db->insert('help_user');
to:
$query = $this->db->insert('help_user', $data);

on your model you must like this
public function add_record($data)
{
$this->db->insert('help_user',$data); //it's more be simple
}
if you write a return; you will return a null value to your controller
if you write like
$query = $this->db->insert('help_user');
you just make a variable $query with value $this->db->insert('help_user'); so thats make a error

hey buddy you are missing one parameter and you are not returning the value, you should have
return $this->db->insert('help_user', $data);

Related

Try to double filter sql query data laravel

guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();

Find values ​in the database starting from a session id

I need display the value in the ga845_clients table ​​in the atacado column, that have 's' or 'n' recorded, but they return blank.
Then when I call <?php echo $this->session->userdata('usu_id');?> in view, the ID is displayed.
I have checked that usu_id is the session ID and the same value from id of table ga845_clientes.
I created the following codes:
Model (Cliente_model.php):
public function getAtacadista($id) {
$this->db->select("atacadista");
$this->db->where('id', $this->session->userdata("usu_id"));
$query = $this->db->get('ga845_clientes');
return $query->result();
}
Controller (Cliente.php):
public function getAtacadista() {
$this->load->model('cliente_model');
$this->load->view("carrinho", $data);
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
}
View (carrinho.php)
echo 'test1:' .$data['atacadista'];
echo 'test2:' .$atacadista;
Remember, $data['atacadista'] dont exists on view, only $atacadista
Also move down the " load view " line, as everton suggested, they are swapped and $data need to be before.
And lastly, as a tip, session userdata can contain a bunch of info, and you should pass the name of the session, not of the value, maybe usu_id is a value inside an array, not the array itself.
You can try the following in your controller
public function getAtacadista() {
$this->load->model('cliente_model');
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
$this->load->view("carrinho", array("data"=>$data));
}
You should place the $data variable assign before the load view.
Because you are not sending your data to the view. change the sequence of your statements.
public function getAtacadista() {
$this->load->model('cliente_model');
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
$this->load->view("carrinho", $data);
}

Calling function with return value in cakephp

I am new to cakephp. I have a problem with calling the function. here is my issue.
In Contrloller file i get all the values using the following function
public function index()
{
$conditions = array(
'order' => array('Histroy.chat_sk DESC')
);
$this->set('histroys', $this->Histroy->find('all',$conditions));
}
In My model file have the following,
class Histroy extends AppModel
{
public $tablePrefix = 'plc_';
public $useTable = 'chat_history';
}
In my view file i have listed the values using foreach() function and that as follows
foreach ($histroys as $histroy):
$oper_name = $histroy['Histroy']['operator_fk'];
$operator_email = $histroy['Histroy']['email'];
endforeach
in that opertaor_fk is a field in history table. So i need get the operator name by another table as operators. So i need to call that function in the view.
Ex : In core we can do like as,
$operator_name = operator_name($fetch['operator_id']);
Function should be like this:
function operator_name($id)
{
// Select the value for the matched field in the operator
return $operator_name;
}
In cakephp how can i retrieve the values.
Please help me out to fix this. Thanks in Advance
Follow the blog tutorial for cake. It'll explain how to create associations and relationships between tables to let you do what is is you want, but in a nutshell, you need to create a relationship between History and Operator models and work from there.

Trying to get property of non-object in controller

This is my controller
public function show($restaurant_id)
{
$data = Restaurant::find($restaurant_id)->waitingtimes();
echo $data->first()->value; exit;
I got Trying to get property of non-object though the waitingtime model is mapped to a database table that has the value column.
Could you help please? Also, could you tell me where can I read the documentation about the returning type of the function find() thanks
Edit 1
The data is not empty; I can see the database, and the restaurant table has the id 20 and the table waitingtimes has values that its restaurant_20 is 20
Actually - what is better is to map the route to a model.
So in your Routes.php file:
Route::model('restaurant', 'Restaurant');
Route::get('/restaurant/{restaurant}', ['as' => 'restaurant.show', 'uses' => RestaurantController#show]);
Then in your controller file:
public function show(Restaurant $restaurant)
{
echo $restaurant->waitingtimes()->first()->value;
}
You can read more about Route Model Binding here.
here is the source of find()
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L643
and here you have the docs
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_find
for your non-object error,
your $data is empty, you should check that before you use the $data collection
Finally I found the solution myself, which is:
public function show($restaurant_id)
{
$data = new Restaurant(array('id' => $restaurant_id));
$data = $data->waitingtimes();
echo $data->first()->value; exit;

Codeigniter RESTful API - How to get parameters

I have this model in my Codeigniter setup:
function get_all_artists(){
$this->db->select('artist_id, artist_name');
$this->db->from('artists');
return $this->db->get();
}
How do I fetch those parameters in my Rest_Controller setup? How would the _get()-function look like?
public function artists_get()
{
???
}
Thanks in advance
Try
public function artists_get(){
$this->load->model('your_model_name');// load model here
$res = $this->your_model_name->get_all_artists();
$result = $res->result_array();// get result as associative array
return json_encode($result);// encode with json
}
With this library you need to append the method type to the function name. _get forGET, _post for POST, _put for PUT, _delete for DELETE. e.g. functionname_get().
As noted in another answer you'll want to retrieve results as an array. So $this->db->get()->result_array();
To retrieve data from the model:
$this->load->model('model_name');
$artists = $this->model_name->get_all_artists();
If you want to send json to front end then you need to do something like this:
$vars['artists'] = json_encode($artists);
$this->_getTemplate('default')->build('artists_page', $vars);
If you are using the REST EXTENSION for CodeIgniter then you must create an object and invoke the method "Respose".
public function artists_get() {
$response = array(
'status'=>TRUE,
'data'=>$result, // This can be an object or array
'obs'=>'all',
);
$this->response($response);
}
That's the official way to do it... and it works perfectly!

Categories