can't update data to database(codeigniter) - php

i have this code in my update function, when i try other value the table updates but when i used that value($field8) the table don't update.
public function edit_lead() {
$id = "3";
$field8 ="http://38.102.225.87/RECORDINGS/MP3/20170201-094849_7188352209-all.mp3";
$this->db->set('field8', $field8);
$this->db->where('id', $id);
$query = $this->db->update('table');
if($query) {
return true;
} else {
return false;
}
}

Simply send the id of the user as a parameter of this function..
public function edit_lead($ID) {
}

Related

How to give “Email does not exist” using this call back function?

I have already tried for "Already exists", but I need help for "Does not exist" instead of "Already exists" using same function.
login.php
public function email_exist($id) {
$this->db->where('email', $id);
$query = $this->db->get('login');
if ($query->num_rows() > 0) {
$this->form_validation->set_message(__FUNCTION__, 'Already exist!');
;
return FALSE;
} else {
return TRUE;
}
}
If the email does not exist in the database, it has to show email does not exists in login page.
Check row exist
Get row having email id, if not get data then return true;
public function email_exist($id) {
$this->db->where('email', $id);
$exist = $this->db->get('login')->row_array();
if (!$exist) {
return true;
}
}
But if you want a login scenario, then you can do as
public function email_exist($id) {
$this->db->where('email', $id);
$exist = $this->db->get('login')->row_array();
if ($exist) {
// START SESSION HERE
return true;
} else {
// give email not exist message
return false;
}
}
if you login via ajax, then you can echo "email_not_exist" from controller
//controller
function email_exist($email) {
$this->load->model('model_name');
$emailArray = $this->model_name->get_data_by_email($email);
if (!empty($emailArray)) {
$this->form_validation->set_message("validate_email", "Email already exists ");
return FALSE;
} else {
return TRUE;
}
}
//model
function get_data_by_email($email) {
return $this->db->where('email', $email)->get('login)->row_array();
}

RETURN values in PHP

I have 2 functions in PHP and i want to return a value to one function to another. this is my functions
public function save_payment_log_data($icasl_number, $exam_session) {
$paylog_av = $this->payment_log_exists($icasl_number, $exam_session);
}
function payment_log_exists($icasl_no, $exam_session) {
$this->db->where('icasl_no', $icasl_no);
$this->db->where('exam_session', $exam_session);
$query = $this->db->get('exm_paymentlog');
if ($query->num_rows() > 0) {
$pl = $query->row();
$pay_id = $pl->paylog_id;
return $pay_id;
} else {
return false;
}
}
I want to return $pay_id to the save_payment_log_data() function but in here $pay_id didn't return to that function. I think it's return from the function payment_log_exists()
so how can I return $pay_id to the save_payment_log_data() function
See this example it's working fine:
function save_payment_log_data() {
$paylog_av = payment_log_exists();
# print the return value
echo $paylog_av;
}
function payment_log_exists() {
return "Hello";
}
save_payment_log_data();
You can try to remove public and this form save_payment_log_data(),
and call the save_payment_log_data() function where you want.
your are only assigning a value to the $paylog_av your "save_payment_log_data" function should be:
public function save_payment_log_data($icasl_number, $exam_session) {
$paylog_av = $this->payment_log_exists($icasl_number, $exam_session);
return $paylog_av;
}

Get PDO lastInsertId() after method called

In my code I use methods to insert data to MySQL database. The method then return either true or false.
If the method return true I would like to use lastInsertId() to run a second method.
Like
if($db->insertMethod($data)){
$lastId = $db->lastInsertId();
$db->secondInsertMethod($lastId, $data2)
}
db.class.php
public function insertMethod($data) {
$db = new DB();
$insert = //Run SQL insert
if($insert > 0 ) {
return true;
}else{
return false;
}
}
public function lastInsertId() {
return $this->pdo->lastInsertId();
}
In this case $db->lastInsertId(); will return 0. One workaround would be to have insertMethod to return this instead of just true but there must another way as well?
Edited!
the solution is correct
The data2 is linked to the data ?
you can also make :
public function insertMethod($data, $data2) {
$db = new DB();
$insert = //Run SQL insert
if($insert > 0 ) {
$this->insertMethod(this->lastInsertId(), $data2);
return true;
} else {
return false;
}
}
or if data2 is linked to the data
public function insertMethod($data) {
$db = new DB();
$insert = //Run SQL insert
if($insert > 0 ) {
$data2 = // get data2
$this->insertMethod(this->lastInsertId(), $data2);
return true;
} else {
return false;
}
}
public function insertMethod($id, $data) {
// your code...
}
or
public function insertMethod($data, $id = null) {
$db = new DB();
if($id == null)
// code to insert without id
else {
$insert = //Run SQL insert
if($insert > 0 ) {
$data2 = // get data2
$this->insertMethod($data2, this->lastInsertId());
}
}
}
The solutions are correct. For you to see who you think is the most appropriate in your situation.

multiple delete with checkbox in ci

I know it has already been asked, but I'm not able to solve this. Below is my control module
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
for ($i = 0; $i < count($did); $i++)
{
$multi = $did[$i];
print_r($multi);
$this->model->delall("user", $multi);
// redirect("control/ahome");
}
}
}
and this is my model module
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in('$wh')");
}
now problem is that it's only delete single row not multiple rows
In your query string, try removing the quotes around the $wh variable.
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in ($wh)");
}
That should get it to work; however, you should be using CodeIgniter's query bindings. Something like this:
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in (?)", array($wh));
}
Refer to this answer for more details.
Try this,
controller,
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
foreach ($did as $d_id)
{
$this->model->delall("user", $d_id);
// redirect("control/ahome");
}
}
}
Model:
public function delall($tb,$wh)
{
$this->db->where('id',$wh);
$this->db->delete($tb);
return true;
}

Displaying default values in codeigniter

In the home page, all subjects should be displayed. When the subject is clicked, their respective chapters has to be displayed. For the first time, that is when the subject is not choosen, then chapters of subject id 1 has to be displayed. In the below code, should i have to call get_chapters() from index and make get_chapters() to return chapters or any other better way of doing it?
class Home extends CI_Controller {
private $data = array();
public function index()
{
$this->load->model('subjects_model');
if($query = $this->subjects_model->get_all()) {
$data['subjects'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(!is_null($id)) {
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
} else {
$query = $this->chapters_model->get('chapters');
}
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
}
Try like
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
If the $id is null then defaltly subject id will become 1 orelse it will search with the existing subject id
public function index($id=null)
{
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}

Categories