How do i get a unique image to MySQL and still be able to edit just one image at a time? I only have the ability to update 4 images at once with a unique id or I can edit the images seperatly and have them update without a unique id.
<?php
require_once('storescripts/connect.php');
mysql_select_db($database_phpimage,$phpimage);
$uploadDir = 'upload/';
if(isset($_POST['upload']))
{
foreach ($_FILES as $file)
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];
if ($fileName != ""){
$filePath = $uploadDir;
$fileName = str_replace(" ", "_", $fileName);
//Split the name into the base name and extension
$pathInfo = pathinfo($fileName);
$fileName_base = $pathInfo['fileName'];
$fileName_ext = $pathInfo['extension'];
//now we re-assemble the file name, sticking the output of uniqid into it
//and keep doing this in a loop until we generate a name that
//does not already exist (most likely we will get that first try)
do {
$fileName = $fileName_base . uniqid() . '.' . $fileName_ext;
} while (file_exists($filePath.$fileName));
$file_names [] = $fileName;
$result = move_uploaded_file($tmpName, $filePath.$fileName);
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[] = $filePath;
}
}
$mid = mysql_real_escape_string(trim($_POST['mid']));
$cat = mysql_real_escape_string(trim($_POST['cat']));
$item = mysql_real_escape_string(trim($_POST['item']));
$price = mysql_real_escape_string(trim($_POST['price']));
$about = mysql_real_escape_string(trim($_POST['about']));
$fields = array();
$values = array();
$updateVals = array();
for($i = 0; $i < 4; $i++)
{
$values[$i] = isset($file_names[$i]) ? mysql_real_escape_string($file_names[$i]) : '';
if($values[$i] != '')
{
$updateVals[] = "{$fields[$i]} = '{$values[$i]}'";
}
}
$updateNames = '';
if(count($updateVals))
{
$updateNames = ", " . implode(', ', $updateVals);
}
$update = "INSERT INTO image
(mid, cid, item, price, about, name1, name2, name3, name4)
VALUES
('$mid', '$cat', '$item', '$price', '$about', '$values[0]', '$values[1]', '$values[2]', '$values[3]')
ON DUPLICATE KEY UPDATE
cid = '$cat', item = '$item', price = '$price', about = '$about' $updateNames";
$result = mysql_query($update) or die (mysql_error());
Within your
foreach ($_FILES as $file) {loop you are renaming and saving the uploaded file but you are using the original filenames when inserting into the db. You need to store the renamed files in the db. After yourdo ... whileloop assign the new filename to an array –then modify your for loop to –
and finally, update your update statement to –