简体   繁体   English

在 mysql 中设置唯一列并在 codeigniter 中显示闪存数据

[英]setting up a unique column in mysql and showing flashdata in codeigniter

i have a codeigniter website, where i have a unique column, users are asked to insert data using input fields, if a user tries to add dupliate value in the column by default the datas wont be inserted, so i am trying to set flash data according to it:我有一个 codeigniter 网站,其中有一个独特的列,要求用户使用输入字段插入数据,如果用户尝试在默认情况下在列中添加重复值,则不会插入数据,所以我正在尝试设置 flash 数据根据它:

function import_domestic_excel_data(){

if(isset($_FILES["file"]["name"])){
   $path = $_FILES["file"]["tmp_name"];
   $object = PHPExcel_IOFactory::load($path);
   foreach($object->getWorksheetIterator() as $worksheet){
       $highestRow = $worksheet->getHighestRow();
       $highestColumn = $worksheet->getHighestColumn();
       for($row=2; $row<=$highestRow; $row++){
 $sendername = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
 $senderreference = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
  if(!empty($sendername)){
               $data[] = array(
                   'sendername'  => $sendername,
                   'senderreference'   => $senderreference,
               );
           }
}
       $response=$this->excel_import_model->insert_excel($data);
   if($response){
    $this->session->set_flashdata("Success","Data Added Successfully !");
    redirect('listconsignment' , 'refresh');
}
  else{
   $this->session->set_flashdata("Error","Data Upload Error !");
    redirect('listconsignment' , 'refresh');
  }
 }
 
}
}

now here the problem is, whatever the outcome like if the data gets uploaded or if the data doesnt get uploaded am only getting the error message set in flashdata, not only flashdata, i tried simply echoing "error" and "success", then also its echoing error if the data is successfully uploaded.现在这里的问题是,无论结果如何,如果数据被上传或数据没有被上传,我只会在 flashdata 中设置错误消息,而不仅仅是 flashdata,我尝试简单地回显“错误”和“成功”,然后也是如果数据上传成功,它会回显错误。 can anyone please tell me what could be wrong in here.谁能告诉我这里可能出了什么问题。 thanks in advance提前致谢

I think the problem is you put the set_flashdata inside foreach :我认为问题是你把set_flashdata放在foreach里面:

foreach ($object->getWorksheetIterator() as $worksheet) {
   ...
}

Maybe there will be always a $response = false on the last $worksheet of the loop.也许在循环的最后一个$worksheet上总会有一个$response = false So it will always error to the session. Take the block to check $response outside foreach will solve this problem:所以它总是会报错到session。在foreach之外的block中检查$response就解决了这个问题:

$result = false;
foreach ($object->getWorksheetIterator() as $worksheet) {
    ....
    $response = $this->excel_import_model->insert_excel($data);
    if ($response) {
        $result = true; // Change to condition to set true to what you want
    }
}
if($result){
    $this->session->set_flashdata("Success", "Data Added Successfully !");
    redirect('listconsignment', 'refresh');
}
else {
    $this->session->set_flashdata("Error", "Data Upload Error !");
    redirect('listconsignment', 'refresh');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM