Page 1 of 1
Need help getting info into database
Posted: Sun Aug 16, 2009 12:56 am
by Flexxall
Hello, I have a php gile which outputs the images in a directory. How can i get the output fro the php file into my database automatically when i run the php script.
Here is what I am currently using for output
Code: Select all
<?php
//SET HERE PATH AND FILES TO DISPLAY
$path = "./";
$files = "*.gif";
$sfiles = glob($path.$files);
asort($sfiles);
foreach ($sfiles as $filename) {
echo "<br>\n", "$filename " . filesize($filename);
}
?>
Re: Need help getting info into database
Posted: Sun Aug 16, 2009 10:13 am
by Flexxall
Ok I have made some progress but I am having an issue with assigning the variables correctly. This also seems to list just one entry in the database. Any help where i messed up ?
Code: Select all
<?php
$path = "./";
$files = "*.gif";
$name = '$filename'; I know these are wrong but dont know how to fix
$size = 'size'; I know these are wrong but dont know how to fix
$type = 'type'; I know these are wrong but dont know how to fix
$sfiles = glob($path.$files);
asort($sfiles);
foreach ($sfiles as $filename) {
echo "<br>\n", "$filename " . filesize($filename);
}
$content = file_get_contents($filename);
if ($conn = mysqli_connect('localhost', 'root', 'xxx', 'xxx')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";
if (mysqli_query($conn, $sql)) {
$uploadOk = true;
$imageId = mysqli_insert_id($conn);
} else {
echo "Error: Could not save the data to mysql database. Please try again.";
}
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
?>
Re: Need help getting info into database
Posted: Wed Aug 19, 2009 5:52 pm
by Flexxall
Any Help with this ?
Re: Need help getting info into database
Posted: Wed Aug 19, 2009 10:19 pm
by Ollie Saunders
Append to the query string in a loop over $sfiles with this or something akin:
Code: Select all
"('{$name}', '{$size}', '{$type}', '{$content}'),"
so you end up with:
SELECT .... ('some', 'values), ('some', 'more'), ('for', 'each'), ('thing', 'you want'),
Remove the illegal final comma with $query = substr($query, 0, -1);