Question

I am trying to make this link work:

<a href='{$_SERVER['PHP_SELF']}?del=true&product_code=".($row['product_code'])."' style='color:black;' onclick='return show_confirm();'>Delete</a> 

It Delete's the specified row from the MSSQL table using the while function. Currently, the bottom code works fine in the sense that it deletes the specific row from the MSSQL table, but I would like it to also unlink a file from the img folder and another file from the specsheets folder.

The file that gets unlinked from img has it's filename stored in the product_img_name column for that table row and the other file has its filename stored in the specsheet column. Each table row has these two columns that contains unique file names from the files that are located in the img, and specsheets folder.

So far when I hit delete I am getting these errors:

Warning: mssql_query() [function.mssql-query]: message: Conversion failed when converting the varchar value 'G1013' to data type int. (severity 16) in D:\Hosting\dl\partscatalogue\partscataloguemanagement.php on line 1175

Warning: mssql_query() [function.mssql-query]: Query failed in D:\Hosting\dtscatalogue\partscataloguemanagement.php on line 1175

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in D:\Hostingdtml\partscatalogue\partscataloguemanagement.php on line 1175

Warning: unlink(img/) [function.unlink]: Permission denied in D:\Hostingdml\partscatalogue\partscataloguemanagement.php on line 1177

Warning: mssql_query() [function.mssql-query]: message: Conversion failed when converting the varchar value 'G1013' to data type int. (severity 16) in D:\Hostingdml\partscatalogue\partscataloguemanagement.php on line 1179

Warning: mssql_query() [function.mssql-query]: Query failed in D:\Hostindhtml\partscatalogue\partscataloguemanagement.php on line 1179

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in D:\Hosting\dhtml\partscatalogue\partscataloguemanagement.php on line 1179

My problem in simple terms is when a table row gets deleted, the file for that row in my website's img folder and the file for that row in my website's specsheets folder remain and do not get deleted with the row.

Here is the code for when the delete link is hit for that specific row:

// delete from table
if ($_GET['del'] == 'true') {
   // cast id as int for security
   $product_code = $_GET['product_code'];
   $fileas = mssql_fetch_array(mssql_query("select product_img_name from products where product_code = $product_code"));
     if (file_exists("img/$fileas")) {
   unlink("img/$fileas");   
     }
   $file38 = mssql_fetch_array(mssql_query("select specsheet from products where product_code = $product_code"));
     if (file_exists("spechsheets/$file38")) {
   unlink("specsheets/$file38"); 
     }
   // delete row from table
   $sql = "DELETE FROM products WHERE product_code = '$product_code'";
   $result = mssql_query($sql, $conn) or die();
   } // end if del

Thank you for any help. All help is greatly appreciated.

Was it helpful?

Solution

I'm not sure what your directory structure is here but for instance you are checking if $fileas exists but then trying to unlink img/$fileas, which is not the same place. If img/$fileas is the correct path, that is also what you need to place in the file_exists() to get the correct check. So it would be file_exists(img/$fileas), etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top