PHP/MySQL updating/deleting file with multiple image links.

Hello, I am new here so I didn’t want to go posting tons of code for help without asking first.

I have started a ‘car dealership inventory’ program for a learning project but im stuck. The program is simple, it allows you to view all vehicle inventory or search the inventory database. Logging in as admin allows y ou to enter the vehicles into the database, as well as modify and/or delete the records.

The code will let users search and view all, it will allow admin to enter records into the database, but it will not allow them to delete or modify the record at all, also using the unlink function it is supposed to delete the images stored in their file locations when the record is deleted from the database.

Is this the kind of thinkg I can get help with here? or is that a bit much?

anyways, I thank you all for your time and help :slight_smile:

quite interesting and easy.

do you need a start up advise/suggestion? or do you have codes already?

Okay, this is the first time I have tried to write a program this big before, as I am still fairly new at the whole PHP thing, I tried it a year or so ago then had to put it on hold until now so please be easy on me if this code is all messed up but constructive criticism is always welcomed . Anyways, The code shows all the data correctly, even the images. When I edit the text fields and hit the ‘edit’ button, it edits the database with no issues.

The problem is, that when ever I try and edit the images, it will not. It does not throw an error or anything, it just simply leaves the images alone. The images int he database are just point to a file location on the web server.(folders are named for the view they are holding, ie. ‘front’ is the folder name for the folder containing the front facing images)

Also, when I delete the record, is there a way to have it delete those images from the linked folder as well? I have messed with the ‘unlink’ function, but it did not seem to work right either.

Thank you guys very much for your time and help.

[php]<?PHP
ini_set(‘display_errors’, 0);
ob_start();
include(‘connection.php’);
$_id = (int)($_GET[‘id’]);
$_sql =“SELECT * FROM veh WHERE id=$_id”;
$_rs = mysql_query($_sql);
while($_rw = mysql_fetch_assoc($_rs))
{
$_id = $_rw[‘id’];
$_year = $_rw[‘year’];
$_make = $_rw[‘make’];
$_model = $_rw[‘model’];
$_style = $_rw[‘style’];
$_mileage = $_rw[‘mileage’];
$_color = $_rw[‘color’];
$_rprice = $_rw[‘rprice’];

$_front = $_rw['front']; 
$_rear = $_rw['rear']; 
 
$_left = $_rw['left']; 
$_right = $_rw['right']; 
 
$_intfront = $_rw['intfront']; 
$_intrear = $_rw['intrear']; 

}

?>

<?PHP if(isset($_POST['edit'])) { $_year = mysql_real_escape_string($_POST['_year']); $_make = mysql_real_escape_string($_POST['_make']); $_model = mysql_real_escape_string($_POST['_model']); $_style = mysql_real_escape_string($_POST['_style']); $_mielage = mysql_real_escape_string($_POST['_mileage']); $_color = mysql_real_escape_string($_POST['_color']); $_rprice = mysql_real_escape_string($_POST['_rprice']); $_id = (int)$_POST['id']; $image_location = $_POST['_front']; $image_location2 = $_POST['_rear']; $image_location3 = $_POST['_left']; $image_location4 = $_POST['_rear']; $image_location5 = $_POST['_intfront']; $image_location6 = $_POST['_intrear']; if(is_uploaded_file($_FILES['front']['tmp_name'])) { $name = $_FILES['front']['name']; $tmp_name = $_FILES['front']['tmp_name']; $location = "front/$name"; move_uploaded_file($tmp_name,$location); } elseif($image_location != 'front/default.jpg'){$location = $image_location;} else{$location = 'front/default.jpg';} $_SQL = "UPDATE `veh` SET `year`='$_year', `make`='$_make', `model`='$_model', `style`='$_style', `mileage`='$_mileage', `color`='$_color', `rprice`='$_rprice', `front`='$_front', `rear`='$_rear', `left`='$_left', `right`='$_right', `intfront`='$_intfront', `intrear`='$_intrear' WHERE `id`='$_id'"; mysql_query($_SQL) or die(mysql_error()); //header("Location : index.php?id=$_id"); $_html =" RECORD HAS BEEN UPDATED CLICK HERE "; } elseif(isset($_POST['delete'])) { $id = (int)$_POST['id']; mysql_query("DELETE FROM `veh` WHERE `id`='$id' LIMIT 1"); $_html = 'Record deleted successfully!
Return to "View All" page.'; } else { $_html="
Year:

  Make:

  Model:


Style:

  Mileage:

  Color


Retail Price:


Front:


        Rear:



Left:


        Right:



Left:


        Right:




"; } ?> <?php echo $_html; ?>[/php]

how many images can a single record have?

6 images. a front, rear, left, right, interior front, and interior rear view.

Oh, and if you dont put in an image, I have a default ‘?’ mark image that will be a place holder until you do put an image there.

cool, and how many tables do you have ? what data you store on each table

only one table, and the image link is all that is stored for the images example: the data for the front image in the database would be front/default.jpg if no image was selected.

The problem is, that when ever I try and edit the images, it will not. It does not throw an error or anything, it just simply leaves the images alone. The images int he database are just point to a file location on the web server.

you have this line on your script
[php]
ini_set(‘display_errors’, 0);
[/php]

if you want to display errors
[php]
error_reporting(E_ALL);
[/php]

replace your whole code with the code below and let me know if it works

[php]

<?PHP //ini_set('display_errors', 0); error_reporting(E_ALL); ob_start(); include('connection.php'); $_id = (int)($_GET['id']); $_sql ="SELECT * FROM `veh` WHERE `id`=$_id"; $_rs = mysql_query($_sql); while($_rw = mysql_fetch_assoc($_rs)) { $_id = $_rw['id']; $_year = $_rw['year']; $_make = $_rw['make']; $_model = $_rw['model']; $_style = $_rw['style']; $_mileage = $_rw['mileage']; $_color = $_rw['color']; $_rprice = $_rw['rprice']; $_front = $_rw['front']; $_rear = $_rw['rear']; $_left = $_rw['left']; $_right = $_rw['right']; $_intfront = $_rw['intfront']; $_intrear = $_rw['intrear']; } ?> <?PHP if(isset($_POST['edit'])) { $_year = mysql_real_escape_string($_POST['_year']); $_make = mysql_real_escape_string($_POST['_make']); $_model = mysql_real_escape_string($_POST['_model']); $_style = mysql_real_escape_string($_POST['_style']); $_mielage = mysql_real_escape_string($_POST['_mileage']); $_color = mysql_real_escape_string($_POST['_color']); $_rprice = mysql_real_escape_string($_POST['_rprice']); $_id = (int)$_POST['id']; $image_location = $_POST['_front']; $image_location2 = $_POST['_rear']; $image_location3 = $_POST['_left']; $image_location4 = $_POST['_right']; $image_location5 = $_POST['_intfront']; $image_location6 = $_POST['_intrear']; if(is_uploaded_file($_FILES['front']['tmp_name'])) { $name = $_FILES['front']['name']; $tmp_name = $_FILES["front"]["tmp_name"]; $location = "front/$name"; move_uploaded_file($tmp_name,$location); } elseif($image_location !== 'front/default.jpg'){$location = $image_location;} else{$location = 'front/default.jpg';} $_SQL = "UPDATE `veh` SET `year`='$_year', `make`='$_make', `model`='$_model', `style`='$_style', `mileage`='$_mileage', `color`='$_color', `rprice`='$_rprice', `front`='$location', `rear`='$image_location2', `left`='$image_location3', `right`='$image_location4', `intfront`='$image_location5', `intrear`='$image_location6' WHERE `id`='$_id'"; mysql_query($_SQL) or die(mysql_error()); //header("Location : index.php?id=$_id"); $_html =" RECORD HAS BEEN UPDATED CLICK HERE "; } elseif(isset($_POST['delete'])) { $id = (int)$_POST['id']; mysql_query("DELETE FROM `veh` WHERE `id`='$id' LIMIT 1"); $_html = 'Record deleted successfully!
Return to "View All" page.'; unlink($_POST['_front']); unlink($_POST['_rear']); unlink($_POST['_right']); unlink($_POST['_left']); unlink($_POST['_intfront']); unlink($_POST['_intrear']); } else { $_html="
Year:

  Make:

  Model:


Style:

  Mileage:

  Color


Retail Price:


Front:


        Rear:



Left:


        Right:



Interior Front:


        Interior Rear:




"; } ?> <?php echo $_html; ?>[/php]

That is awesome, much more progress. It will let you now change the image for front and rear, but not the others. Also it deletes the images when you delete the file, which is great, but it also deletes that default.jpg image I put in there for when you don’t have an image for the car.

in order for the script to be able to change other images you need 5 more statements like this
[php]

if(is_uploaded_file($_FILES[‘front’][‘tmp_name’]))
{
$name = $_FILES[‘front’][‘name’];
$tmp_name = $_FILES[“front”][“tmp_name”];
$location = “front/$name”;
move_uploaded_file($tmp_name,$location);
}
elseif($image_location !== ‘front/default.jpg’){$location = $image_location;}
else{$location = ‘front/default.jpg’;}
[/php]

as you can you only had one statement which is for the front image. i assumed you only needed the front so i didnt implement it lol

lol, Sorry about that. I left that code out because it just kept throwing errors for me earlier.

[php]if(is_uploaded_file($_FILES[‘rear’][‘tmp_name’]))
{
$name = $_FILES[‘rear’][‘name’];
$tmp_name = $_FILES[“rear”][“tmp_name”];
$location2 = “rear/$name”;
move_uploaded_file($tmp_name,$location2);
}
elseif($image_location2 !== ‘front/default.jpg’){$location2 = $image_location2;}
else{$location2 = ‘front/default.jpg’;} [/php]

I added that code underneath the one above it, So I would be able to modify the ‘rear’ image, as well. However, with this code, if I select an image for the ‘rear’ slot, it still changes the front image instead.

show me what you got as now so i can be more accurate with my asnwer

Here you go, and like I said, the ‘front’ image updates when you update the front image position or the rear, with this code. and the dellete seems to delete the default.jpg as well.

[php]<?PHP
//ini_set(‘display_errors’, 0);
error_reporting(E_ALL);

ob_start();

include(‘connection.php’);

$_id = (int)($_GET[‘id’]);
$_sql =“SELECT * FROM veh WHERE id=$_id”;
$_rs = mysql_query($_sql);
while($_rw = mysql_fetch_assoc($_rs))
{
$_id = $_rw[‘id’];
$_year = $_rw[‘year’];
$_make = $_rw[‘make’];
$_model = $_rw[‘model’];
$_style = $_rw[‘style’];
$_mileage = $_rw[‘mileage’];
$_color = $_rw[‘color’];
$_rprice = $_rw[‘rprice’];

$_front = $_rw['front']; 
$_rear = $_rw['rear']; 
 
$_left = $_rw['left']; 
$_right = $_rw['right']; 
 
$_intfront = $_rw['intfront']; 
$_intrear = $_rw['intrear']; 

}

?>

<?PHP if(isset($_POST['edit'])) { $_year = mysql_real_escape_string($_POST['_year']); $_make = mysql_real_escape_string($_POST['_make']); $_model = mysql_real_escape_string($_POST['_model']); $_style = mysql_real_escape_string($_POST['_style']); $_mielage = mysql_real_escape_string($_POST['_mileage']); $_color = mysql_real_escape_string($_POST['_color']); $_rprice = mysql_real_escape_string($_POST['_rprice']); $_id = (int)$_POST['id']; $image_location = $_POST['_front']; $image_location2 = $_POST['_rear']; $image_location3 = $_POST['_left']; $image_location4 = $_POST['_right']; $image_location5 = $_POST['_intfront']; $image_location6 = $_POST['_intrear']; if(is_uploaded_file($_FILES['front']['tmp_name'])) { $name = $_FILES['front']['name']; $tmp_name = $_FILES["front"]["tmp_name"]; $location = "front/$name"; move_uploaded_file($tmp_name,$location); } elseif($image_location !== 'front/default.jpg'){$location = $image_location;} else{$location = 'front/default.jpg';} if(is_uploaded_file($_FILES['rear']['tmp_name'])) { $name = $_FILES['rear']['name']; $tmp_name = $_FILES["rear"]["tmp_name"]; $location2 = "rear/$name"; move_uploaded_file($tmp_name,$location2); } elseif($image_location2 !== 'front/default.jpg'){$location2 = $image_location2;} else{$location2 = 'front/default.jpg';} $_SQL = "UPDATE `veh` SET `year`='$_year', `make`='$_make', `model`='$_model', `style`='$_style', `mileage`='$_mileage', `color`='$_color', `rprice`='$_rprice', `front`='$location', `rear`='$image_location2', `left`='$image_location3', `right`='$image_location4', `intfront`='$image_location5', `intrear`='$image_location6' WHERE `id`='$_id'"; mysql_query($_SQL) or die(mysql_error()); //header("Location : index.php?id=$_id"); $_html =" RECORD HAS BEEN UPDATED CLICK HERE "; } elseif(isset($_POST['delete'])) { $id = (int)$_POST['id']; mysql_query("DELETE FROM `veh` WHERE `id`='$id' LIMIT 1"); $_html = 'Record deleted successfully!
Return to "View All" page.'; unlink($_POST['_front']); unlink($_POST['_rear']); unlink($_POST['_right']); unlink($_POST['_left']); unlink($_POST['_intfront']); unlink($_POST['_intrear']); } else { $_html="
Year:

  Make:

  Model:


Style:

  Mileage:

  Color


Retail Price:


Front:


        Rear:



Left:


        Right:



Interior Front:


        Interior Rear:




"; } ?> <?php echo $_html; ?>[/php]

in your query

change
[php]
rear=’$image_location2’,
[/php]

to

[php]
rear=’$location2’,
[/php]

you mean change this line

[php]$image_location2 = $_POST[’_rear’]; [/php]

to

[php]$location2 = $_POST[’_rear’]; [/php]

well yeah change that as well but i what i mean is change it in your query

[php]
$_SQL = "UPDATE veh SET year=’$_year’,
make=’$_make’,
model=’$_model’,
style=’$_style’,
mileage=’$_mileage’,
color=’$_color’,
rprice=’$_rprice’,

                        `front`='$location', 
                        `rear`='$image_location2', 
                         
                        `left`='$image_location3', 
                        `right`='$image_location4', 
                         
                        `intfront`='$image_location5', 
                        `intrear`='$image_location6' 
                         
                  WHERE `id`='$_id'"; 

[/php]
so change this

Haha, I can be really slow sometimes. I got it now :slight_smile: The images update great, as well as the rest of the data. And the images delete like they should now. I don’t suppose you could point me in the right direction to making that default.jpg image not get deleted?

I’d like it to stay there so if there are no images to upload, the user won’t be looking at a red ‘X’.

And thank you very very much for all your help on this.

I made a small function to help you

listen/read carefullyfollowing these steps

[ol][li]insert this function[php]function GetFileName($f)
{
/*

  • this function will get the name of a file
  • for Instance if the file is image14.jpg
  • only image14 will be returned.

BY wilson382
*/
$h=substr($f,strrpos($f,"/"),strrpos($f,"."));
$FileName= substr($h,1,strrpos($h,".")-1);

return $FileName;
}[/php]
below this line in your script[php] include(‘connection.php’);[/php][/li]
[li]look for these code in your script[php] unlink($_POST[’_front’]);
unlink($_POST[’_rear’]);
unlink($_POST[’_right’]);
unlink($_POST[’_left’]);
unlink($_POST[’_intfront’]);
unlink($_POST[’_intrear’]);[/php]
and replace them with
[php]
if (“default” != strtolower(GetFileName($_POST[’_front’]))){
unlink($_POST[’_front’]);
}
if (“default” != strtolower(GetFileName($_POST[’_rear’]))){
unlink($_POST[’_rear’]);
}
if (“default” != strtolower(GetFileName($_POST[’_right’]))){
unlink($_POST[’_right’]);
}
if (“default” != strtolower(GetFileName($_POST[’_left’]))){
unlink($_POST[’_left’]);
}
if (“default” != strtolower(GetFileName($_POST[’_intfront’]))){
unlink($_POST[’_intfront’]);
}
if (“default” != strtolower(GetFileName($_POST[’_intrear’]))){
unlink($_POST[’_intrear’]);
}[/php][/li]
[li]Save the file and see if still works lol[/li]
[li]the default image should not be deleted[/li]
[li]you are done[/li][/ol]

Sponsor our Newsletter | Privacy Policy | Terms of Service