Need help getting info into database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Flexxall
Forum Newbie
Posts: 3
Joined: Sun Aug 16, 2009 12:53 am

Need help getting info into database

Post 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);
}
?>
Flexxall
Forum Newbie
Posts: 3
Joined: Sun Aug 16, 2009 12:53 am

Re: Need help getting info into database

Post 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.";
        }
?>
Flexxall
Forum Newbie
Posts: 3
Joined: Sun Aug 16, 2009 12:53 am

Re: Need help getting info into database

Post by Flexxall »

Any Help with this ?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Need help getting info into database

Post 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);
Post Reply