Jump to content

Unlinking of images,and updates error...


rootKID

Recommended Posts

Hello W3 People!. i have downloaded a picture upload class from here: verot.net.and working perfectly for my kind of coding in the schools. but i have never tried to make a (UPDATE) page of this kind of stuff where its deleting a picture and then uploading a new one dynamicly... and it does uploads the images correctly 100% and resizing them, and stuff like that... but it wont delete the older pictures, and update the Database Tables/Rows with the new image URL (witch are just the name of the the picture... im defining the path the diffrent places myself...) ideas?... here is my code [A LITTLE LONG...]:

<?phpsession_start();ob_start();require_once("../db_conn.php");require_once("../login_checker_noroot.php");require_once("pic_mobel_uploader/class/class.upload.php");echo "<a href='index.php'>Tilbage Til Forsiden</a>"; if(!empty($_GET['m_id'])){$mid = $_GET['m_id'];$query = "SELECTdesigner.designer_navn,serie.serie_navn,produkter.design_aar,produkter.pris,produkter.vare_nr,produkter.information,produkter.produkt_navnFROMprodukter  INNER JOIN serie ON produkter.FK_serie = serie.id  INNER JOIN designer ON produkter.FK_designer = designer.d_id   WHERE produkter.id = $mid";$result = mysql_query($query)or die(mysql_error());while($row = mysql_fetch_array($result,MYSQL_ASSOC)){  $p_navn = $row['produkt_navn'];  $serie = $row['serie_navn'];  $designer = $row['designer_navn'];  $v_nr = $row['vare_nr'];  $aar = $row['design_aar'];  $pris = $row['pris'];  $tekst = $row['information'];}//While Statement Ends...  if(isset($_POST['Submit'])){$p_navnn = mysql_real_escape_string($_POST['p_navn']);$seriee = mysql_real_escape_string($_POST['serie']);$designerr = mysql_real_escape_string($_POST['designer']);$v_nrr = mysql_real_escape_string($_POST['v_nr']);$aarr = mysql_real_escape_string($_POST['aar']);$priss = mysql_real_escape_string($_POST['pris']);$tekstt = mysql_real_escape_string($_POST['tekst']);/*===================================*//*===================================*//*===================================*/if($_FILES['image'] >= 1)//bigger then one...{$pID = $_GET['m_id'];$queryImg = "SELECT * FROM image_url WHERE FK_produkt_id = " . $pID;$resultImg = mysql_query($queryImg)or die(mysql_error());while($rowImg = mysql_fetch_array($resultImg)){unlink('../../images/home_news_form/'.$rowImg['img_url']);unlink('../../images/mobel_details_big/'.$rowImg['img_url']);unlink('../../images/mobel_details_thumps_small/'.$rowImg['img_url']);unlink('../../images/mobel_liste_form/'.$rowImg['img_url']);}// as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files$files = array();foreach ($_FILES['image'] as $k => $l){foreach ($l as $i => $v){  if(!array_key_exists($i, $files))  $files[$i] = array();  $files[$i][$k] = $v;}}// now we can loop through $files, and feed each element to the classforeach ($files as $file){if(!empty($_FILES['image']))//allways form name...{// we instanciate the class for each element of $file$handle = new Upload($file);//$file for the new (array images upload) to work...//$handle = new Upload($_FILES['image']); //OLD ONE... if ($handle->uploaded){  $handle->image_resize		  = true;  $handle->image_ratio		   = true;  $handle->image_x			   = 110; //Width in Pixels...  $handle->image_y			   = 80; //Height in Pixels...    $handle->Process('../../images/home_news_form/');}if ($handle->uploaded){  $handle->image_resize		  = true;  $handle->image_ratio		   = true;  $handle->image_x			   = 300; //Width in Pixels...  $handle->image_y			   = 225; //Height in Pixels...    $handle->Process('../../images/mobel_details_big/');}if ($handle->uploaded){  $handle->image_resize		  = true;  $handle->image_ratio		   = true;  $handle->image_x			   = 50; //Width in Pixels...  $handle->image_y			   = 50; //Height in Pixels...    $handle->Process('../../images/mobel_details_thumps_small/');}if ($handle->uploaded){  $handle->image_resize		  = true;  $handle->image_ratio		   = true;  $handle->image_x			   = 120; //Width in Pixels...  $handle->image_y			   = 90; //Height in Pixels...   $handle->Process('../../images/mobel_liste_form/');$image = $handle->file_dst_name;$produktID = $_GET['m_id'];  mysql_query("UPDATE image_url SET	    img_url='$image',	    FK_produkt_id='$produktID'	    WHERE FK_produkt_id = ".$pID)or die(mysql_error());}}//end empty (FILES) statement...}//end foreach statement...}//if (FILES['image']) ends.../*===================================*//*===================================*//*===================================*/$query = "UPDATE produkter SET	   produkt_navn = '$p_navnn',	   FK_serie = '$seriee',	   FK_designer = '$designerr',	   vare_nr = '$v_nrr',	   design_aar = '$aarr',	   pris = '$priss',	   information = '$tekstt'	    WHERE id = $mid";mysql_query($query)or die(mysql_error());header("location:index.php");}//submit ends...}//IF !empty Statement Ends...else{  $p_navn = "NO ID SELECTED!...";  $serie = "NO ID SELECTED!...";  $designer = "NO ID SELECTED!...";  $v_nr = "NO ID SELECTED!...";  $aar = "NO ID SELECTED!...";  $pris = "NO ID SELECTED!...";  $tekst = "NO ID SELECTED!...";}?> <fieldset><legend>Redigering Af Vare</legend><form method="post" action="" enctype="multipart/form-data"><table border="1" align="center"><tr><td>Produkt Navn</td><td><input type="text" name="p_navn" value="<?php echo $p_navn; ?>" /></td></tr><tr><td>Serie</td><td><select name="serie"><option value="0">Vælg Serie</option><?php    $m_query = "SELECT * FROM serie";    $m_result = mysql_query ($m_query)or die(mysql_error());    while ($m_row = mysql_fetch_array ($m_result))    {	    echo "<option value='" .$m_row['id']."'>" .$m_row['serie_navn']."</option>";    }?></select></td></tr><tr><td>Designer</td><td><select name="designer"><option value="0">Vælg Designer</option><?php$d_query = "SELECT * FROM designer";$d_result = mysql_query ($d_query)or die(mysql_error());while ($d_row = mysql_fetch_array ($d_result)){  echo "<option value='" .$d_row['d_id']."'>" .$d_row['designer_navn']."</option>";}?></select></td></tr><tr><td>Vare Nummer</td><td><input type="text" name="v_nr" value="<?php echo $v_nr; ?>" /></td></tr><tr><td>Aar</td><td><input type="text" name="aar" value="<?php echo $aar; ?>" /></td></tr><tr><td>Pris</td><td><input type="text" name="pris" value="<?php echo $pris; ?>" /></td></tr><?php$produkt_id = $_GET['m_id'];$query2 = "SELECT * FROM image_url WHERE FK_produkt_id = $produkt_id";$result2 = mysql_query($query2)or die(mysql_error());while($row2 = mysql_fetch_array($result2)){?><tr><td><?php echo "<img src='../../images/mobel_liste_form/".$row2['img_url']."'>"; ?></td><td><input type="file" name="image[]" id="image" /></td></tr><?php}?><tr><td>Teksten Her</td><td><textarea type="text" name="tekst" rows="10" cols="40" /><?php echo $tekst; ?></textarea></td></tr><tr><td> </td><td><input type="submit" name="Submit" value="Rediger Nu!" /></td></tr></table></form></fieldset>

Hoping answers soon!... thanks (rootKID)... PS: any questions i need to answer, just ask... and ill try... [AND SORRY, BUT THE MOST OF THE CODING STYLE IS MADE IN DANISH, BUT I AM FROM DENMARK AFTERALL... I HAVE ALSO TRIED TO COMMENT OUT SOME (MOST OF IT) THE STUFF IN ENGLISH, HOPE IT HELPS...)

Link to comment
Share on other sites

Have you printed out those file names you're trying to delete to verify that they are correct? Have you verified that the database query you're using to look up the old images is returning the correct records? What about the update queries, are you printing those out to verify that they are correct? And why do you keep setting $_GET['m_id'] to multiple variables?

Link to comment
Share on other sites

okay, i did knew about the variables (GET)... i will fix that part soon... and yes, i have also writed the echoes out to check names, and stuff like that... and they (ARE) being uploaded... but the DB is no being updated... (the img_url)... EDIT: the old images is not going to be deleted also... so the DB is not updating, and the Old image file that was going to be deleted, is not going to be actually deleted... but still uploads the other pictures... :/...

Edited by rootKID
Link to comment
Share on other sites

If the image path is correct and unlink is not deleting it, then you may not have permission to delete those files. Regardless, if unlink doesn't work it will output an error message. If you're not seeing that error message then you need to make sure that error reporting is enabled.

Link to comment
Share on other sites

hmm... about that, i dont remember how to enable it...could you tell me how?... im on xampp latest version.... PS: i dont know if im getting any value at all into the images that needs to be deleted... but still, the while is inside a FOREACH statement...so i will also try checking that later... but not sure if im right in that part still, what do you think?.. :/...

Link to comment
Share on other sites

I don't like using while loops when you're only retrieving one row from the database. Loops are for doing the same things multiple times. If I'm expecting a query to return a single result then I check if it returned a result and use that row, or else I show an error because the record wasn't found in the database when it should have been. I don't want to continue processing if the record isn't even in the database, I want to show an error and quit. That's just semantics though, the syntax of your code is legal. You can enable error reporting like this: ini_set('display_errors', 1);error_reporting(E_ALL);

Link to comment
Share on other sites

hello, sorry for late reply...have finded a solution on this! :)... -thanks for the help! :)..

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...