0

我有一些将 CSV 文件上传到指定文件夹的代码,但它不会更新数据库。

public function do_upload()
{
    $csv_path = realpath(APPPATH . '/../assets/uploads/CSV/');
    $config['upload_path']   = $csv_path;
    $config['allowed_types'] = '*'; // All types of files allowed
    $config['overwrite']     = true; // Overwrites the existing file

    $this->upload->initialize($config);
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->layout->buffer('content', 'program/upload', $error);
        $this->layout->render();
    }
    else
    {


        $image_data = $this->upload->data();
        $fname = $image_data['file_name'];
        $fpath = $image_data['file_path'].$fname;
        $fh = fopen($fpath, "r");



        $insert_str = 'INSERT INTO wc_program (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n";


        if ($fh) {

            // Create each set of values.
            while (($csv_row = fgetcsv($fh, 2000, ',')) !== false) {

                foreach ($csv_row as &$row) {
                    $row = strtr($row, array("'" => "\'", '"' => '\"'));
                }

                $insert_str .= '("'
                    // Implode the array and fix pesky apostrophes.
                    .implode('","', $csv_row)
                    .'"),'."\n";
            }

            // Remove the trailing comma.
            $insert_str = rtrim($insert_str, ",\n");

            // Insert all of the values at once.
            $this->db->set($insert_str);

            echo '<script type="text/javascript">
                        alert("Document successfully uploaded and saved to the database.");
                        location = "program/index";
                </script>';
        } 
        else {
                echo '<script type="text/javascript">
                        alert("Sorry! Something went wrong please proceed to try again.");
                        location = "program/upload";
                </script>';
        } 


    }
}

当我运行 var_dump($fh); 它显示:资源(89)类型(流)

当我运行var_dump($fpath)它显示:string(66) "/Applications/MAMP/htdocs/site/assets/uploads/CSV/wc_program.csv"

所以这一切都上传了,但它不更新数据库有什么问题?我尝试了各种更改fopen方法,但仍然没有乐趣,我真的需要将它添加到数据库中,插入查询和设置查询应该可以解决问题,但它没有。

非常感谢任何帮助!

4

1 回答 1

1

您没有在数据库上运行任何查询。您将活动记录语法与简单查询语法混合在一起。活动记录插入查询将通过调用来执行。

$this->db->insert('my_table');

db::set()实际上并不查询数据库。它接受一个键/值对,该键/值对将在db::insert()或被db::update()调用之后插入或更新。如果您自己构建查询,则需要使用该db::query()函数。

查看活动目录文档

您可以使用$this->db->query('put your query here'),但您失去了 CodeIgniter 内置安全性的好处。查看CodeIgniter 的查询函数。

我将为您提供使用 CodeIgniter 插入数据库的多种方法中的几种示例。这些示例将根据您的评论生成查询。您将需要相应地调整您的代码。

示例 1

$result = $this->db
    ->set('JobRef', 911847)
    ->set('Area', 'Coastal')
    ->set('Parish', 'Yapton')
    ->set('AbbrWorkType', 'Micro')
    ->set('WorkType', 'Micro-Asphalt Surfacing')
    ->set('Timing', 'TBC')
    ->set('TrafficManagement', 'No Positive Traffic Management')
    ->set('Location', 'Canal Road (added PMI 16/07/12)')
    ->set('Duration', '2 days')
    ->set('Start', '0000-00-00')
    ->set('Finish', '0000-00-00') 
    ->insert('wc_program');

echo $this->db->last_query() . "\n\n";
echo "RESULT: \n\n";
print_r($result);

示例 2(使用关联数组):

$row = array(
    'JobRef' => 911847,
    'Area' => 'Coastal',
    'Parish' => 'Yapton',
    'AbbrWorkType' => 'Micro',
    'WorkType' => 'Micro-Asphalt Surfacing',
    'Timing' => 'TBC',
    'TrafficManagement' => 'No Positive Traffic Management',
    'Location' => 'Canal Road (added PMI 16/07/12)',
    'Duration' => '2 days',
    'Start' => '0000-00-00',
    'Finish' => '0000-00-00'
);
$this->db->insert('wc_program', $row);
// This will do the same thing
// $this->db->set($row);
// $this->db->insert('wc_program');
echo $this->db->last_query();

示例 1 和 2 使用的是 Active Record。信息被一块一块地存储,然后在您进行最终调用时构建查询。这有几个优点。它允许您动态构建查询,而无需担心 SQL 语法和关键字的顺序。它还会转义您的数据。

示例 3(简单查询):

$query = 'INSERT INTO 
    wc_program 
        (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) 
    VALUES 
        ("911847","Coastal","Yapton","Micro","Micro-Asphalt Surfacing","TBC","No Positive Traffic Management","Canal Road (added PMI 16/07/12)","2 days","0000-00-00","0000-00-00")';
$result = $this->db->query($query);
echo $this->db->last_query() . "\n\n";
echo "RESULT: \n";
print_r($result);

这种方式将所有针对注入的保护留给您,可能导致更多错误,并且更难更改/维护。

如果你打算这样做,你应该使用以下语法,这将防止注入。

示例 4

$query = 'INSERT INTO 
    wc_program 
        (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) 
    VALUES 
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';

$row = array(
    911847,
    'Coastal',
    'Yapton',
    'Micro',
    'Micro-Asphalt Surfacing',
    'TBC',
    'No Positive Traffic Management',
    'Canal Road (added PMI 16/07/12)',
    '2 days',
    '0000-00-00',
    '0000-00-00'
);

$result = $this->db->query($query, $row);
echo $this->db->last_query() . "\n\n";
echo "RESULT: \n";
print_r($result);

CodeIgniter 将替换每个“?” 转义后,在查询中使用数组中的相应值。您可以使用它来运行许多具有相同形式但具有不同数据的查询,只需更新$row数组并受益于 CI 的内置安全性。

于 2013-04-04T20:10:14.280 回答