如何使用php和sql重新排序图像(按路径)



我有几个关于在sql数据库中移动和删除图像(路径和名称)的问题。

以下是我上传和命名图像的方式:

if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//Grab file extension
$extension = pathinfo($_FILES['upload']['name'][$i], PATHINFO_EXTENSION);
//save the filename
$shortname = $name . "_" . $i . "." .$extension;
//save the url and the file
//Modify this to year, make, _, id, #
$filePath = "images/" . $shortname;
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db 
//use $shortname for the filename
}
}
}
} //end images

然后我取$files[#],将图像名称和路径插入数据库,然后将图像上传到images目录中。我的问题是,如果我想从数据库中删除图像或更改保存顺序,我该如何操作?

要从数据库中删除,请使用DELETE语句:

DELETE FROM myTable where id='$id'

DELETE FROM myTable where name='$name'

您需要确保您正在尝试删除正确的行。如果没有primary_key,那么name列应该是UNIQUE

关于订单,您的数据库中没有订单,至少没有明确的订单。如果您需要一个订单,请添加一列,比如order,并使用ORDER BY子句进行查询:

SELECT * FROM myTable ORDER BY order ASC

使用此选项,您的订单中不会出现任何缺口问题。

最新更新