如何使用php将值从txt导入到mysql db(pdo)

时间:2018-11-22 13:03:18

标签: php mysql file pdo

我想将一些值从txt文件导入到我的数据库中。文件结构如下所示:

Title: Blazing Saddles
Release Year: 1974
Format: VHS
Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahn

Title: Casablanca
Release Year: 1942
Format: DVD
Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre

Title: Charade
Release Year: 1953
Format: DVD
Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy

我使用这种形式:

<!DOCTYPE html>
<html>
<head>
    <link href="css/forms.css" rel="stylesheet">
    <title>Add film</title>
    <?php require 'menu.php'; ?>
</head>
<body>
<h2>Add films from .txt file</h2>
<p>If you want to add new films to our collection, you need to fill in the form below</p>
<div class="container">
    <form action="importAction.php" method="post" enctype="multipart/form-data">
            <input name="file" type="file" style="margin-bottom: 10px" accept=".txt">
            <div class="row">
                <input type="submit" value="Submit">
            </div>
    </form>
</div>
</body>
</html>

这种向我的数据库添加新项目的方法:

public function addFilm($name, $year, $format, $actors)
    {
        return $this->db->setQuery("INSERT INTO {$this->config[self::DB_NAME]} 
        (name, year, format, actors) VALUES ('{$name}', '{$year}', '{$format}', '{$actors}')");
    }

我该怎么做?你能帮我吗?

1 个答案:

答案 0 :(得分:0)

文件方法是一个好主意,我使用了一种非常简单的RegEx方法来从文件中读取数据:

$regex = '/Title:\s(.*)\r\nRelease Year:\s(.*)\r\nFormat:\s(.*)\r\nStars:\s(.*)\r\n/';

$matches = [];
preg_match_all($regex, $text, $matches, PREG_SET_ORDER);

// need validation for matches structure of course

foreach ($matches as $match) {
    $this->addFilm($match[1], $match[2], $match[3], $match[4]);
}

匹配结果如下

    /*
    array:3 [
      0 => array:5 [
        0 => """
          Title: Blazing Saddles\r\n
          Release Year: 1974\r\n
          Format: VHS\r\n
          Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahn\r\n
          """
        1 => "Blazing Saddles"
        2 => "1974"
        3 => "VHS"
        4 => "Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahn"
      ]
      1 => array:5 [
        0 => """
          Title: Casablanca\r\n
          Release Year: 1942\r\n
          Format: DVD\r\n
          Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre\r\n
          """
        1 => "Casablanca"
        2 => "1942"
        3 => "DVD"
        4 => "Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre"
      ]
      2 => array:5 [
        0 => """
          Title: Charade\r\n
          Release Year: 1953\r\n
          Format: DVD\r\n
          Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy\r\n
          """
        1 => "Charade"
        2 => "1953"
        3 => "DVD"
        4 => "Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy"
      ]
    ]
     */

请考虑到正则表达式不是非常安全的错误。任何错误的字符,缺少行尾等都可能导致错误。 但这仅是示范,您如何实现。