Hi guys i have a nice working PHP script which reads the image files in a directory on my server and insert the imagepath in database. It is working fine but i need to insert more values not only the imagepath also a category and a name but i cant figure out how to make the query work the right way it only iserts the image itself. I want to insert the whole path instead of only the image with extension and 2 other values like name and category.
Here is the PHP script
<?php
$server = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbname';
$connect = mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname,$connect);
$path = "folder/subfolder/";
$files = array_map('mysql_real_escape_string',array_map('basename',array_filter(glob("{$path}*.*"),'is_file')));
if(empty($files)){
echo "There were no matching files to insert into the database.";
} else {
$query = "INSERT INTO name (picurl) VALUES ('" . implode("'),('",$files) . "')";
if(!mysql_query($query)){
echo "There was a problem inserting the data.";
trigger_error("Query failed: $query<br />Error: " . mysql_error());
} else {
echo "The data was inserted successfully.";
}
}?>
What i need is something like that in the query
$query = "INSERT INTO gbpics (picname, picacat, picurl) VALUES ('" . implode("'),('",$files) . "')";
And replace the image with extension with the whole part plus the image and extension. Any Ideas?
Some help would be great. Thanks
Figured it out here is the solution script:
<?php
$server = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbname';
$connect = mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname,$connect);
$path = "folder/subfolder/";
$files = array_map('mysql_real_escape_string',array_filter(glob("{$path}*.*"),'is_file'));
if(empty($files)){
echo "There were no matching files to insert into the database.";
} else {
$insertValues = array();
foreach($files as $file)
{
$insertValues[] = "('value1', 'value2', '{$file}')";
}$query = "INSERT INTO `name` (`picname`, `piccat`, `picurl`) VALUES " . implode(', ', $insertValues);
if(!mysql_query($query)){
echo "There was a problem inserting the data.";
trigger_error("Query failed: $query<br />Error: " . mysql_error());
} else {
echo "The data was inserted successfully.";
}
}?>