如何在MySQL中获得一个唯一的映像,并且一次只能编辑一个映像?我只能用唯一的id一次更新4个图像,或者我可以单独编辑图像,并在没有唯一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());
在foreach ($_FILES as $file) {
循环中,您重命名并保存上传的文件,但在插入数据库时使用原始文件名。您需要将重命名的文件存储在数据库中。在do ... while
循环之后,将新文件名分配给数组-
do {
$fileName = $fileName_base . uniqid() . '.' . $fileName_ext;
} while (file_exists($filePath.$fileName));
$file_names[] = $fileName;
然后将for循环修改为-
for($i = 0; $i < 4; $i++)
{
$values[$i] = isset($filenames[$i]) ? mysql_real_escape_string($filenames[$i]) : '';
if($values[$i] != '') {
$updateVals[] = 'name' . ($i + 1) . " = '{$values[$i]}'";
}
}
最后,将您的更新声明更新为-
$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";