如何使用 if else 条件在 php mysql 数据库中导入 csv 数据

Posted

技术标签:

【中文标题】如何使用 if else 条件在 php mysql 数据库中导入 csv 数据【英文标题】:How import csv data in php mysql database with if else condition 【发布时间】:2015-12-06 06:51:45 【问题描述】:

我需要在我的产品表有两列代码和价格的数据库中导入 csv 数据。我正在使用此脚本导入数据,该脚本会找到产品代码,然后更新该产品价格 -

function update_price()

    $this->sma->checkPermissions('csv');
    $this->load->helper('security');
    $this->form_validation->set_rules('userfile', lang("upload_file"), 'xss_clean');

    if ($this->form_validation->run() == true) 



        if (isset($_FILES["userfile"])) 

            $this->load->library('upload');

            $config['upload_path'] = $this->digital_upload_path;
            $config['allowed_types'] = 'csv';
            $config['max_size'] = $this->allowed_file_size;
            $config['overwrite'] = TRUE;

            $this->upload->initialize($config);

            if (!$this->upload->do_upload()) 

                $error = $this->upload->display_errors();
                $this->session->set_flashdata('error', $error);
                redirect("products/update_price");
            

            $csv = $this->upload->file_name;

            $arrResult = array();
            $handle = fopen($this->digital_upload_path . $csv, "r");
            if ($handle) 
                while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
                    $arrResult[] = $row;
                
                fclose($handle);
            
            $titles = array_shift($arrResult);

            $keys = array('code', 'price');

            $final = array();

            foreach ($arrResult as $key => $value) 
                $final[] = array_combine($keys, $value);
            
            $rw = 2;
            foreach ($final as $csv_pr) 
                if (!$this->products_model->getProductByCode(trim($csv_pr['code']))) 
                    $this->session->set_flashdata('message', lang("check_product_code") . " (" . $csv_pr['code'] . "). " . lang("code_x_exist") . " " . lang("line_no") . " " . $rw);
                    redirect("product/update_price");
                
                $rw++;
            
        

    

    if ($this->form_validation->run() == true && !empty($final)) 
        $this->products_model->updatePrice($final);
        $this->session->set_flashdata('message', lang("price_updated"));
        redirect('products');
     else 

        $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));

        $this->data['userfile'] = array('name' => 'userfile',
            'id' => 'userfile',
            'type' => 'text',
            'value' => $this->form_validation->set_value('userfile')
        );

        $bc = array(array('link' => base_url(), 'page' => lang('home')), array('link' => site_url('products'), 'page' => lang('products')), array('link' => '#', 'page' => lang('update_price_csv')));
        $meta = array('page_title' => lang('update_price_csv'), 'bc' => $bc);
        $this->page_construct('products/update_price', $meta, $this->data);

    

但是这里我们的产品价格更新并替换旧值,但我想检查旧值的价格和 如果旧值大于上传值,则不替换并且 如果旧值低于上传的价格值,则更新/替换该值。 意味着在所有情况下我们的价格总是最高的。 我们怎么能做到?任何人都请帮忙..

【问题讨论】:

这里有人帮不了我吗????请给出提示...任何提示 【参考方案1】:

我认为您应该查看updatePrice 方法并在其中修改一个mysql 查询。

Mysql 有一个正确的Update if 语句。

【讨论】:

但是它如何检查值是更大还是更低 看规范或实例

以上是关于如何使用 if else 条件在 php mysql 数据库中导入 csv 数据的主要内容,如果未能解决你的问题,请参考以下文章

php if else条件在变量中作为字符串[重复]

PHP if...else...elseif 语句

PHP控制语句之“if...else”语句

PHP 结构语句

如何在多个if / else条件下使用Throws?

如何避免在 .where() 函数中使用 IF-Else 并使用内联 if 条件?