So I am working on this little script that will take a directory listing of just folder names within directory, and INSERT them in to a table on sql database. I am REALLY close, but my variable of my string that I formatted the results to be multiple entries to the sql command, for some reason are only taking the Last item of the list.
What is puzzling me is checking my work, I echo $speed after it grabs the directory listing, etc, which looks like (this folder),(another foldr name),(lastfolder name), etc ... If I copy and paste that instead of $speed after VALUES in my sql script, it inserts the list just fine, but using the $speed variable, it Only inserts the Last of the list, as in (lastfolder name) for example above.. Here is the script I'm workin on. Any help is kindly appreciated and respected!
<?php
$iterator = new DirectoryIterator('FOLDER');
foreach ($iterator as $fileinfo) {
if ($fileinfo->isDir()) {
$speed = "(" . "'" . $fileinfo->getFilename() . "'" . ")" . "," . "\n";
echo $speed;
}
}
$servername = "localhost";
$username = "nathan";
$password = "mypasswrd";
$dbname = "datadatabase";
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);
$sql = "INSERT IGNORE INTO senior_dir (studentcodedir)
VALUES $speed('andthis')";
// 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;
?>`
note: I am aware this is similar to this post here, but mine is hitting a bump and wondering if anyone could have insight what may be happening to me: Insert a php string as multiple rows on MySQL table