0

There is my database:

CREATE TABLE Persons (
ID int AUTO_INCREMENT,
Father varchar(255) NOT NULL,
Text varchar(255),
PRIMARY KEY (ID));

And there is my .txt file: (NULL, '1', 'text'),(NULL, '2', 'text'),(NULL, '3', 'text')...

Ok this works for me now:

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$text_file = file_get_contents('newfile.txt');
    $sql = "INSERT INTO `Persons` (`ID`, `Father`, `Text`) VALUES".$text_file;
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

And now how do i upload multiple txt files, like from multiple folders, or just one folder?

David
  • 33
  • 6
  • which database? have you any code to show? - by the way you have to get rid of the NULL values – Eineki Jul 04 '17 at 17:17
  • No im pretty new to this, instead of writing it i want upload text into database thats all – David Jul 04 '17 at 17:29
  • Possible duplicate of [Insert Blobs in MySql databases with php](https://stackoverflow.com/questions/7052655/insert-blobs-in-mysql-databases-with-php) – Marged Jul 04 '17 at 20:41

1 Answers1

0

Try it like this:

$text_file = file_get_contents('myTextFile.txt');
$sql = "INSERT INTO Persons (ID,Father,Text)VALUES".$text_file;

This goes only if your textfile has only this content you posted (NULL, '1', 'text'),(NULL, '2', 'text'),(NULL, '3', 'text').

Take care for SQL injection.

Richard
  • 618
  • 1
  • 9
  • 15