I have a few questions regarding moving and removing image (path and name) in a sql database.
Here's how I upload and name the images:
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
Then I take the $files[#] and insert the image name and path into the database and the image is uploaded into the images directory. My question would be, if I want to remove an image from the database or change the order it would be saved, how would I go about doing that?
For removing from database, use DELETE
statement:
DELETE FROM myTable where id='$id'
Or
DELETE FROM myTable where name='$name'
You need to be sure that you are trying to delete the correct row. If you don't have a primary_key, your name
column should be UNIQUE
.
About order, there is no order in your database, at least not explicit. If you need to have an order, add a column, let's say, order
and query with ORDER BY
clause:
SELECT * FROM myTable ORDER BY order ASC
You will not have any problem with gaps in your order using this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.