0

Hey guys so here is my table:

    CREATE TABLE mytable (
ID int AUTO_INCREMENT,
Name varchar(255) NOT NULL,
PRIMARY KEY (ID));

There is my code:

<?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);

// begin the transaction
$conn->beginTransaction();
// our SQL statements
$size = "";
$dir = "test";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
        if($file == '.' || $file == '..') continue;

    $myFile = "$dir/$file";
    $fh = fopen($myFile, 'r');

    $size = filesize($myFile);
    if ($size > 0) {
    $content = fread($fh, $size);

    $title = $file; 
    echo $title."</br>";
    }
$models = explode("\r",$content);
$conn->exec("INSERT INTO `mytable` (`Name`) 
VALUES".$models); 

    fclose($fh);
}
closedir($handle);
}

// commit the transaction
$conn->commit();
echo "Succsess";
}
catch(PDOException $e)
{
// roll back the transaction if something failed
$conn->rollback();
echo "Error: " . $e->getMessage();
}

$conn = null;
?>

Here is how my text file looks:

4745G
VN7-572
E5-511G
VN7-571....

And i keep getting this error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

Anyone know what i did wrong and how to fix it?

David
  • 33
  • 6

0 Answers0