In my I am trying to retrieve the last_insert (ID) from a query true calling a function here is where i call my function:
$lead = $_leadh->addLead($lead_data, $call_data['number'], $user);
$LeadID = $lead['id'];
I am not getting any result? This is my addLead function:
function addLead(array $lead_data, $number, $user) {
//check if lead with same number and name exist in db
if ($checker = >= 1)
{
//return lead id as existed
$data = $query->fetch_array(MYSQLI_ASSOC);
return array('id'=>$data['lead_id'], 'exists'=>true);
}
else
{
//insert new lead into db
if ($query = $this->QueryDB("INSERT", "INTO leads (lead_name)
VALUES ('".$this->EscapeString($lead_data['lead_name'])))
{
//return lead id as new
return array('id'=>$this->insert_id, 'exists'=>false);
}
else
{
//output error if insertion fail
return false;
}
}}
You are returning array from addLead function but while calling you are not getting in any variable:
$_leadh->addLead($lead_data, $call_data['number'], $user);
$LeadID = $lead['id'];
should be like below:
$lead = $_leadh->addLead($lead_data, $call_data['number'], $user);
$LeadID = $lead['id'];
Related
after I inserting data, what shows on the column table "Jumlah Hasil Perah" shows '0'. but after refreshing the browser, the value shows up the result.
here's the code
Model (m_hasilperah):
public function jumlahPerahSapi($id)
{
$this->db->select('hasilPerahPagi, hasilPerahSore');
$this->db->where('idSapi', $id);
$cek = $this->db->get('tb_hasilperah');
if ($cek) {
$this->db->set('jumlahPerah', "hasilPerahPagi + hasilPerahSore", FALSE);
$this->db->where('idSapi', $id);
$this->db->update('tb_hasilperah');
}
return false;
}
Controller
public function tambahHasilPerah(){
$idSapi = $this->input->post('idSapi');
$tglPerah = $this->input->post('tglPerah');
$hasilPerahPagi = $this->input->post('hasilPerahPagi');
$hasilPerahSore = $this->input->post('hasilPerahSore');
$jumlahPerah = $this->m_hasilperah->jumlahPerahSapi($idSapi);;
$data =
[
'idSapi' => $idSapi,
'tglPerah' => $tglPerah,
'hasilPerahPagi' => $hasilPerahPagi,
'hasilPerahSore' => $hasilPerahSore,
'jumlahPerah' => $jumlahPerah
];
$insert = $this->m_hasilperah->tambahHasilPerahModel($data, $idSapi);
if ($insert) {
redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh');
} else {
echo 'gagal';
}
}
Screenshot:
View after insert(before manual refresh)
View after manual refresh
Your Model method jumlahPerahSapi() every time returned false. Even if after the data update. Please look into this.
Your Model function jumlahPerahSapi($id) is always return false even inserted successfully in DB.
So This is not run redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh');
Every time run echo 'gagal';
Modify the model function as follows.
public function jumlahPerahSapi($id)
{
$this->db->select('hasilPerahPagi, hasilPerahSore');
$this->db->where('idSapi', $id);
$cek = $this->db->get('tb_hasilperah');
if ($cek) {
$this->db->set('jumlahPerah', "hasilPerahPagi + hasilPerahSore", FALSE);
$this->db->where('idSapi', $id);
$this->db->update('tb_hasilperah');
return true;
}
return false;
}
So I'm currently trying to do a PDO SELECT Request, but when executing and fetching the extracted data, this error shows up:
1 - Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:\wamp64\www\NewKali\includes\user.inc.php on line 53
2 - Error: Call to a member function fetch() on boolean in C:\wamp64\www\NewKali\includes\user.inc.php on line 53
This is where I call the function:
include 'includes/user.inc.php';
$userOBJ = new User;
if($userOBJ->isAdmin($_SESSION['session_u-name'])){
AdminControl();
}
Code:
public function isAdmin($user){
$userToGet = $user;
$stmt = $this->Connect()->prepare("SELECT * FROM user_secure WHERE username_db=?");
$query1 = $stmt->execute([$userToGet]);
if(!$query1)
{
die("Execute query error, because: ". print_r($this->Connect()->errorInfo(),true) );
}else{
foreach ($query1->fetch() as $row) {
if($row['admin_db'] == 1){
return true;
} else {
return false;
}
}
}
}
The first error says that I'm not handling the PDO errors, which I think that I'm already handling any PDO error in my code, but somehow still gets detected as I'm not doing so... (Correct me if wrong)
Second error states that calling PDO->fetch() is returning a boolean, but I'm requesting data, so it's not able to continue with the following code...
I don't get why this is showing... The "username_db" var in the query is the same as the one that I have in my DB.
In the same file as the function above, I have this next function and when called, it does fine
public function RegisterUser($user, $pwd, $mail){
$u_Insert = $user;
$p_Insert = $pwd;
$m_Insert = $mail;
$stmt = $this->Connect()->prepare("INSERT INTO user_secure(username_db, password_db) VALUES (?,?)");
$query1 = $stmt->execute([$u_Insert, $p_Insert]);
$stmt = $this->Connect()->prepare("INSERT INTO user_info(mail_db) VALUES (?)");
$query2 = $stmt->execute([$m_Insert]);
if($query2 && $query1){
return true;
} else {
return false;
}
}
Is there something that I'm missing?
I have already checked this thread but I'm still in the exact position...
Thank you for your time
(I'm still learning PDO, sorry if my code isn't clean)
This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.
$query1 = $stmt->execute([$userToGet]);
In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.
foreach ($query1->fetch() as $row) {
if($row['admin_db'] == 1){
return true;
} else {
return false;
}
}
To get the row, you need to write it like this:
$results = $stmt->fetch();
or in your case:
foreach ( $stmt->fetch() as $row) {
if($row['admin_db'] == 1){
return true;
} else {
return false;
}
}
I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
So here's what I want to do. I want to check if the userid in segment(3) exist or else it will redirect somewhere instead of still loading the view with an error.
Here's the example url
http://localhost/ems/edit_user/edit_user_main/1001
Now if I try to edit the userid in segment(3) and intentionally put an invalid userid, it still loads the view and i don't know why
Here's my function
public function edit_user_main(){
$id = $this->uri->segment(3);
$check = $this->get_data->check_if_exist($id);
if($check) {
$data['title'] = 'Edit User';
$data['id'] = $this->session->userdata('usertoedit');
$this->load->model('accounts/get_data');
$item = $this->get_data->get_user($id);
$data['user'] = $item[0];
$data['main_content'] = 'edit_user/edit_user_main';
$this->load->view('includes/template', $data);
} else {
redirect('admin/adminuser');
}
}
Here's the model
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query) {
return TRUE;
} else {
return FALSE;
}
}
There is no problem with the fetching of data.
The problem is even if the userid doesn't exist, the view is still loading but with an error coz there's no data for that userID. It's not redirecting,
I tried using print_r and it working fine, the value of the $check is 1 when there's a valid userID.
Hope someone can help me with this. Thank you
With your function it will always return true because the statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always execute,So you need to check query is returning any result row or not with the statement
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
Try this..
With the function it will always return true because the following statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always be execute, So need to check query is returning any result row or not
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
And load heper as:-
$this->load->helper('url');
before the redirection
I have faced a problem .I want to select all rows by executing this function:
public function executeQuery($query,$fetch_mode=null) {
$rs = null;
if ($stmt = $this->getConnection()->prepare($query)) {
if ($this->executePreparedStatement($stmt, $rs,$fetch_mode)) {
return $rs;
}
} else {
throw new DBError($this->getConnection()->errorInfo());
}
}
private function executePreparedStatement($stmt, & $row = null,$fetch_mode=null) {
$boReturn = false;
if($fetch_mode==null) $fetch_mode=$this->fetch_mode;
if ($stmt->execute()) {
if ($row = $stmt->fetch($fetch_mode)) {
$boReturn = true;
} else {
$boReturn = false;
}
} else {
$boReturn = false;
}
return $boReturn;
}
But when I call it from my index page:
$objDB=new DB();
$objDB->connect();
// executeQuery returns an array
$result=$objDB->executeQuery("SELECT * FROM admin");
var_dump($result);
Only a single row is retrieved instead of all rows.
I also set mode using:
$result=$objDB->executeQuery("SELECT * FROM admin",PDO::FETCH_ASSOC);
But it still does not work.
The fetch method returns only the current row and sets the row pointer to the next row. To read all data in a PHP array you can use fetchAll().
Additionally return-by-reference is no good idea in PHP as it messes with PHP's copy-on-write mechanism and often creates trouble.
So I'd write oyure code something like this:
public function executeQuery($query,$fetch_mode=null) {
if ($stmt = $this->getConnection()->prepare($query)) {
$ret = $this->executePreparedStatement($stmt, $fetch_mode);
return $ret;
}
throw new DBError($this->getConnection()->errorInfo());
}
private function executePreparedStatement($stmt, $fetch_mode=null) {
if($fetch_mode==null) $fetch_mode=$this->fetch_mode;
if ($stmt->execute()) {
if ($rows = $stmt->fetchAll($fetch_mode)) {
return $rows;
}
}
return false;
}