How to import values from txt to mysql db(pdo) with php [closed]











up vote
-1
down vote

favorite












I want to import some values to my db from txt file. File scructure looks like this:



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


I use this form:



<!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>


And this method to add new items to my db:



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}')");
}


How can I do that? Can you, please, help me with that?










share|improve this question















closed as too broad by yivi, AdrianHHH, sideshowbarker, Makyen, Pearly Spencer Nov 23 at 1:17


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • You could use file(..) to read the textfile line by line, then use explode(': ',$line) with some logic to get the values behind the colon.
    – Michel
    Nov 22 at 13:28















up vote
-1
down vote

favorite












I want to import some values to my db from txt file. File scructure looks like this:



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


I use this form:



<!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>


And this method to add new items to my db:



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}')");
}


How can I do that? Can you, please, help me with that?










share|improve this question















closed as too broad by yivi, AdrianHHH, sideshowbarker, Makyen, Pearly Spencer Nov 23 at 1:17


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • You could use file(..) to read the textfile line by line, then use explode(': ',$line) with some logic to get the values behind the colon.
    – Michel
    Nov 22 at 13:28













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to import some values to my db from txt file. File scructure looks like this:



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


I use this form:



<!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>


And this method to add new items to my db:



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}')");
}


How can I do that? Can you, please, help me with that?










share|improve this question















I want to import some values to my db from txt file. File scructure looks like this:



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


I use this form:



<!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>


And this method to add new items to my db:



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}')");
}


How can I do that? Can you, please, help me with that?







php mysql file pdo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 13:10









Maxim Fedorov

2,105316




2,105316










asked Nov 22 at 13:03









John Rapid

34




34




closed as too broad by yivi, AdrianHHH, sideshowbarker, Makyen, Pearly Spencer Nov 23 at 1:17


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as too broad by yivi, AdrianHHH, sideshowbarker, Makyen, Pearly Spencer Nov 23 at 1:17


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • You could use file(..) to read the textfile line by line, then use explode(': ',$line) with some logic to get the values behind the colon.
    – Michel
    Nov 22 at 13:28


















  • You could use file(..) to read the textfile line by line, then use explode(': ',$line) with some logic to get the values behind the colon.
    – Michel
    Nov 22 at 13:28
















You could use file(..) to read the textfile line by line, then use explode(': ',$line) with some logic to get the values behind the colon.
– Michel
Nov 22 at 13:28




You could use file(..) to read the textfile line by line, then use explode(': ',$line) with some logic to get the values behind the colon.
– Michel
Nov 22 at 13:28












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










The file approach is a good idea, I used a very simple RegEx approach to read the data from the file:



$regex = '/Title:s(.*)rnRelease Year:s(.*)rnFormat:s(.*)rnStars:s(.*)rn/';

$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]);
}


Output of matches is as follows



    /*
array:3 [
0 => array:5 [
0 => """
Title: Blazing Saddlesrn
Release Year: 1974rn
Format: VHSrn
Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahnrn
"""
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: Casablancarn
Release Year: 1942rn
Format: DVDrn
Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorrern
"""
1 => "Casablanca"
2 => "1942"
3 => "DVD"
4 => "Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre"
]
2 => array:5 [
0 => """
Title: Charadern
Release Year: 1953rn
Format: DVDrn
Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedyrn
"""
1 => "Charade"
2 => "1953"
3 => "DVD"
4 => "Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy"
]
]
*/


Please take into consideration that the regex is not very error safe. Any wrong character, missing line ending etc. can lead to an error.
But this is only for demonstration, how you can do it.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    The file approach is a good idea, I used a very simple RegEx approach to read the data from the file:



    $regex = '/Title:s(.*)rnRelease Year:s(.*)rnFormat:s(.*)rnStars:s(.*)rn/';

    $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]);
    }


    Output of matches is as follows



        /*
    array:3 [
    0 => array:5 [
    0 => """
    Title: Blazing Saddlesrn
    Release Year: 1974rn
    Format: VHSrn
    Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahnrn
    """
    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: Casablancarn
    Release Year: 1942rn
    Format: DVDrn
    Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorrern
    """
    1 => "Casablanca"
    2 => "1942"
    3 => "DVD"
    4 => "Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre"
    ]
    2 => array:5 [
    0 => """
    Title: Charadern
    Release Year: 1953rn
    Format: DVDrn
    Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedyrn
    """
    1 => "Charade"
    2 => "1953"
    3 => "DVD"
    4 => "Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy"
    ]
    ]
    */


    Please take into consideration that the regex is not very error safe. Any wrong character, missing line ending etc. can lead to an error.
    But this is only for demonstration, how you can do it.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      The file approach is a good idea, I used a very simple RegEx approach to read the data from the file:



      $regex = '/Title:s(.*)rnRelease Year:s(.*)rnFormat:s(.*)rnStars:s(.*)rn/';

      $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]);
      }


      Output of matches is as follows



          /*
      array:3 [
      0 => array:5 [
      0 => """
      Title: Blazing Saddlesrn
      Release Year: 1974rn
      Format: VHSrn
      Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahnrn
      """
      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: Casablancarn
      Release Year: 1942rn
      Format: DVDrn
      Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorrern
      """
      1 => "Casablanca"
      2 => "1942"
      3 => "DVD"
      4 => "Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre"
      ]
      2 => array:5 [
      0 => """
      Title: Charadern
      Release Year: 1953rn
      Format: DVDrn
      Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedyrn
      """
      1 => "Charade"
      2 => "1953"
      3 => "DVD"
      4 => "Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy"
      ]
      ]
      */


      Please take into consideration that the regex is not very error safe. Any wrong character, missing line ending etc. can lead to an error.
      But this is only for demonstration, how you can do it.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        The file approach is a good idea, I used a very simple RegEx approach to read the data from the file:



        $regex = '/Title:s(.*)rnRelease Year:s(.*)rnFormat:s(.*)rnStars:s(.*)rn/';

        $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]);
        }


        Output of matches is as follows



            /*
        array:3 [
        0 => array:5 [
        0 => """
        Title: Blazing Saddlesrn
        Release Year: 1974rn
        Format: VHSrn
        Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahnrn
        """
        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: Casablancarn
        Release Year: 1942rn
        Format: DVDrn
        Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorrern
        """
        1 => "Casablanca"
        2 => "1942"
        3 => "DVD"
        4 => "Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre"
        ]
        2 => array:5 [
        0 => """
        Title: Charadern
        Release Year: 1953rn
        Format: DVDrn
        Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedyrn
        """
        1 => "Charade"
        2 => "1953"
        3 => "DVD"
        4 => "Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy"
        ]
        ]
        */


        Please take into consideration that the regex is not very error safe. Any wrong character, missing line ending etc. can lead to an error.
        But this is only for demonstration, how you can do it.






        share|improve this answer












        The file approach is a good idea, I used a very simple RegEx approach to read the data from the file:



        $regex = '/Title:s(.*)rnRelease Year:s(.*)rnFormat:s(.*)rnStars:s(.*)rn/';

        $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]);
        }


        Output of matches is as follows



            /*
        array:3 [
        0 => array:5 [
        0 => """
        Title: Blazing Saddlesrn
        Release Year: 1974rn
        Format: VHSrn
        Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahnrn
        """
        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: Casablancarn
        Release Year: 1942rn
        Format: DVDrn
        Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorrern
        """
        1 => "Casablanca"
        2 => "1942"
        3 => "DVD"
        4 => "Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre"
        ]
        2 => array:5 [
        0 => """
        Title: Charadern
        Release Year: 1953rn
        Format: DVDrn
        Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedyrn
        """
        1 => "Charade"
        2 => "1953"
        3 => "DVD"
        4 => "Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy"
        ]
        ]
        */


        Please take into consideration that the regex is not very error safe. Any wrong character, missing line ending etc. can lead to an error.
        But this is only for demonstration, how you can do it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 15:43









        Philipp Palmtag

        1,05521117




        1,05521117















            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo