Yangilangan eng yangi rekord ID in codeIgniter?

Men bu erda taxminan 5 soat ishlashgan, ammo muvaffaqiyatsiz bo'lgan boshqa bir so'rovga keldim. Quyidagi kabi rasm jadvalga egaman.

Ma'lumotni muvaffaqiyatli kiritdim.

Now I want to update the inserted data by using below view, controller, and Model classes. My View class:

 <form role="form" method="post" enctype="multipart/form-data" action="<?php echo base_url('admin/updateCategory'); ?>">
          <div class="box-body">
            <div class="form-group">
     <input type="hidden" value='<?php echo $catDataForEdit->cat_id; ?>' name="id_hidden">
              
              <input type="text" class="form-control" id="exampleInputEmail1" name="category_name" placeholder="Category Name" value='<?php echo $catDataForEdit->category_name; ?>' >
            </div>

            <div class="form-group">
              
              <!--input type="text" class="form-control" id="exampleInputEmail1" placeholder="Category Icon"-->
              
Icon Size: Width: 26px, Height: 28px and Icon Format: .png

<input type="file" name="cat_icon" value='<?php echo $catDataForEdit->iconName; ?>'> </div> <div class="form-group">
Image Size: Width: 175px, Height: 120px and Image Format: .png

<input type="file" name="cat_image" value='<?php echo $catDataForEdit->imageName; ?>'> </div> </div> <!-- /.box-body --> <div class="box-footer box-footer-border"> <div class="row"> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> <input type="submit" class="btn btn-primary" name="update_cat_submit" value="Modify"> </div> </div>

my Controller Admin.php:

 public function updateCategory(){
    $data = array();
    $targetDir = "images/dynamic/";
    $prevcatIconName; $prevCatImageName;
    $catIconName; $category_name; $catImageName; 
    $targetFilePathIcon; $targetFilePathImage; 
    $date; $unix_time;$hidden_id;
    $hidden_id = $this->input->post('id_hidden');
    if ($this->input->post('update_cat_submit')) {

        $this->form_validation->set_rules('category_name', 'Category Name', 'trim|required');
      if ($this->form_validation->run() == false) {
        $this->modifyCategory($hidden_id);
      }else{

          $category_name = $this->input->post('category_name');


    //Category Icon
    if ( !empty($_FILES["cat_icon"]["name"])) {
      //getting values from view
         $catIconName = basename($_FILES["cat_icon"]["name"]);
         $targetFilePathIcon = $targetDir . $catIconName;
         $iconFileType = pathinfo($targetFilePathIcon,PATHINFO_EXTENSION);


      //allow certain file formats
      $allowTypes = array('jpg','png','jpeg','gif');
      if(in_array($iconFileType, $allowTypes)){
          //upload file to server
          if(move_uploaded_file($_FILES["cat_icon"]["tmp_name"], $targetFilePathIcon)){
            //echo "The file ".$fileName. " has been uploaded.";


              //Category Image
            if ( !empty($_FILES["cat_image"]["name"])) {
               $catImageName = basename($_FILES["cat_image"]["name"]);
               $targetFilePathImage = $targetDir . $catImageName;
               $imageFileType = pathinfo($targetFilePathImage,PATHINFO_EXTENSION); 
              //echo $catIconName . ','. $catImageName;
                //allow certain file formats
              $allowTypes = array('jpg','png','jpeg','gif');
              if(in_array($imageFileType, $allowTypes)){
                  //upload file to server
                  if(move_uploaded_file($_FILES["cat_image"]["tmp_name"], $targetFilePathImage)){
                //echo "The file ".$fileName. " has been uploaded.";
                     $date = date('Y-m-d H:i:s');
                     $unix_time=human_to_unix($date);

                //insert Category Icon into db and get CatIconId
                    $fields = array(
                  'image_name' => $catIconName,
                  'modified_at' => $unix_time,
                  'status'     => 'a'

                  );
                    //print_r($fields);
                 $iconId = $this->img_model->updateImages($fields); 
                  //echo $iconId;

                //insert Category Image
                    $fields = array(
                  'image_name' => $catImageName,
                  'modified_at' => $unix_time,
                  'status'     => 'a'

                  );

              $imageId = $this->img_model->updateImages($fields);
              //echo $iconId . ',' .$imageId;
                echo $imageId;
                //echo $unix_time;
                if ($imageId != "" && $iconId != "" ) {
                  $next_fields = array(

                    'cat_name' => $category_name,
                    'cat_icon' => $iconId,
                    'cat_image' =>$imageId,
                    'modified_at'=>$unix_time,
                    'status' =>'a'

                    );
                //print_r($next_fields); die();
                  $result = $this->cat_model->updateCatNames($hidden_id,$next_fields);
                if ($result) {
                    //$this->Category();
                    redirect(base_url('admin/Category'));
                    //$this->load->view('admin/category');

                }else{
                  $data['error_msg'] = 'there is problem with your input';
                }

            }


              }else{


                  $data['error_msg'] = 'Sorry, there was an error uploading your image.';
                 $this->modifyCategory($hidden_id);
              }//move_uploaded_file if else loop close
          }else{

              $data['error_msg'] = 'Sorry, only JPG, JPEG, PNG, GIF images are allowed to upload.';
              $this->modifyCategory($hidden_id);
          }//in_array if else loop close
              }else{

                $data['error_msg'] = 'Please select a image to upload.';
               $this->modifyCategory($hidden_id);
              }//empty file verification close

          }else{

             $data['error_msg'] = 'Sorry, there was an error uploading your Icon.';
             $this->modifyCategory($hidden_id);
          }//move_uploaded_file if else loop close
      }else{

          $data['error_msg'] = 'Sorry, only JPG, JPEG, PNG, GIF icons are allowed to upload.';
          $this->modifyCategory($hidden_id);
      }//in_array if else loop close
  }else{

      $data['error_msg'] = 'Please select a icon to upload.';
      $this->load->view('admin/modify-category', $data);
    }
  }
}


} 

My Model Class Img_model:

 public function updateImages($fields){
    $id = $this->input->post('id_hidden');

    $this->db->where('id', $id);
    $query = $this->db->update('images', $fields);

   if($this->db->affected_rows() > 0){

      $this->db->where($fields);
     $result = $this->db->get('images')->row()->id;
     //echo $result;
     return $result;

     //I have tried like below to get the last insert id but unable to get.
     //return $this->db->insert_id();
   }else{

    return false;
   }   
}//close update Images

My Model Cat_model:

 public function updateCatNames($hidden_id,$next_fields){
 $this->db->where('id', $hidden_id);
  $this->db->update('categories', $next_fields);
  if($this->db->affected_rows() > 0){
    return true;
  }else{
    return false;
  } 
  }

I have tried to get the latest inserted record id from my Img_model class, but unable to get it. Can anyone help out please? Thanks in advance!

0
Javobingiz uchun tashakkur, men xatoni aniqlanmagan ikkinchisini sinab ko'rdim: result_array ().
qo'shib qo'ydi muallif Madhu Kumar, manba
Javobingiz uchun tashakkur, men xatoni aniqlanmagan ikkinchisini sinab ko'rdim: result_array ().
qo'shib qo'ydi muallif Madhu Kumar, manba

6 javoblar

First Try this

Ikkinchidan, jadval tasvirlaringizda cat_id maydoni mavjud emas. Tasvirlar va toifalar jadvali o'rtasidagi aloqani qanday yaratasiz? Quyidagi misol rasmini ko'ring

enter image description here

Bir property_images jadvali bor va bu erda property_id bor, mulkning bir nechta rasmiga ega bo'lishim mumkin. Ammo sizning rasmlaringiz jadvalida category_id qo'yish uchun faqat bitta toifadagi tasvirga ega bo'lishingiz mumkin, chunki sizning so'rovlaringiz strukturasini ancha osonlashtiradi.

Siz yuborgan kategoriyali identifikatorga ega bo'lgan yozuvni yangilashingiz kerak va agar u sizning nomingizdan ro'yxatdan o'tishni xohlasangiz, to'liq yozuvni qaytarib olasiz.

Your Case

     $fields = array(
     'image_name' => $catImageName,
     'modified_at' => $unix_time,
     'status'     => 'a'
     );
     $imageId = $this->img_model->updateImages($fields);

Yuqoridagi kodni siz kategoriyangizning ID-ni sizning ko'rinishingizdan yuboradigan rasm jadvalini yangilash uchun ishlatasiz

<input type="hidden" value='<?php echo $catDataForEdit->cat_id; ?>' name="id_hidden">

va tasvir modelingizdan ushbu rasm toifasini id yozuvlarini yangilash uchun olasiz

$id = $this->input->post('id_hidden');

$this->db->where('id', $id);
$query = $this->db->update('images', $fields);

Va hozir siz yangilagan rasm identifikatorini olishni istaysiz va bu kodni bu kodni olishingiz mumkin

 $st=$this->db->select('*')->from('images')->WHERE('image_name',$fields['image_name'])->get()->result_array();
 $updatedRecordId=$st[0]['id']
//Now you can return it or use it. 

Lekin bu men uchun mantiqiy emas.

0
qo'shib qo'ydi

First Try this

Ikkinchidan, jadval tasvirlaringizda cat_id maydoni mavjud emas. Tasvirlar va toifalar jadvali o'rtasidagi aloqani qanday yaratasiz? Quyidagi misol rasmini ko'ring

enter image description here

Bir property_images jadvali bor va bu erda property_id bor, mulkning bir nechta rasmiga ega bo'lishim mumkin. Ammo sizning rasmlaringiz jadvalida category_id qo'yish uchun faqat bitta toifadagi tasvirga ega bo'lishingiz mumkin, chunki sizning so'rovlaringiz strukturasini ancha osonlashtiradi.

Siz yuborgan kategoriyali identifikatorga ega bo'lgan yozuvni yangilashingiz kerak va agar u sizning nomingizdan ro'yxatdan o'tishni xohlasangiz, to'liq yozuvni qaytarib olasiz.

Your Case

     $fields = array(
     'image_name' => $catImageName,
     'modified_at' => $unix_time,
     'status'     => 'a'
     );
     $imageId = $this->img_model->updateImages($fields);

Yuqoridagi kodni siz kategoriyangizning ID-ni sizning ko'rinishingizdan yuboradigan rasm jadvalini yangilash uchun ishlatasiz

<input type="hidden" value='<?php echo $catDataForEdit->cat_id; ?>' name="id_hidden">

va tasvir modelingizdan ushbu rasm toifasini id yozuvlarini yangilash uchun olasiz

$id = $this->input->post('id_hidden');

$this->db->where('id', $id);
$query = $this->db->update('images', $fields);

Va hozir siz yangilagan rasm identifikatorini olishni istaysiz va bu kodni bu kodni olishingiz mumkin

 $st=$this->db->select('*')->from('images')->WHERE('image_name',$fields['image_name'])->get()->result_array();
 $updatedRecordId=$st[0]['id']
//Now you can return it or use it. 

Lekin bu men uchun mantiqiy emas.

0
qo'shib qo'ydi

Buni ko'ring:

if( $this->db->update('categories', $next_fields) )
{
    echo $hidden_id;
}

As $this->db->update() return TRUE/FALSE on successful/failed execution of query. So the above code echo $hidden_id only when query successfully executes.

0
qo'shib qo'ydi
Tez javob uchun tashakkur .. Katta kodni Cat_model'dan emas, Img_model faylidan so'nggi qo'shish kerak.
qo'shib qo'ydi muallif Madhu Kumar, manba

Buni ko'ring:

if( $this->db->update('categories', $next_fields) )
{
    echo $hidden_id;
}

As $this->db->update() return TRUE/FALSE on successful/failed execution of query. So the above code echo $hidden_id only when query successfully executes.

0
qo'shib qo'ydi
Tez javob uchun tashakkur .. Katta kodni Cat_model'dan emas, Img_model faylidan so'nggi qo'shish kerak.
qo'shib qo'ydi muallif Madhu Kumar, manba

Modelingiz sinfida Img_model:

public function updateImages($fields){
    $id = $this->input->post('id_hidden');

    $this->db->where('id', $id);
    $query = $this->db->update('images', $fields);

   if($this->db->affected_rows() > 0){
        return $id;
       //$id is your updated record id because it is unique.
   }else{
    return false;
   }   
}
0
qo'shib qo'ydi

Modelingiz sinfida Img_model:

public function updateImages($fields){
    $id = $this->input->post('id_hidden');

    $this->db->where('id', $id);
    $query = $this->db->update('images', $fields);

   if($this->db->affected_rows() > 0){
        return $id;
       //$id is your updated record id because it is unique.
   }else{
    return false;
   }   
}
0
qo'shib qo'ydi
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin