Jump to content

Recommended Posts

Hi guys, the issue with my PHP script at the moment is I want it to die if the file cannot be found in the database, this is my current code:

 

<?php
$file = $_GET["id"];
$delid = $_GET["delid"];
include "my_sql.php";
mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die;
$myFile = "./images/$file";
unlink($myFile);
?>

 

I put 'or die' after the delete function, but this is totally being ignored and the unlink function is being executed. Please help I want the script to stop if it cannot find anything in the database.

Link to comment
https://forums.phpfreaks.com/topic/136368-solved-die-function-code-not-exiting/
Share on other sites

Hi guys, the issue with my PHP script at the moment is I want it to die if the file cannot be found in the database, this is my current code:

 

<?php
$file = $_GET["id"];
$delid = $_GET["delid"];
include "my_sql.php";
mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die;
$myFile = "./images/$file";
unlink($myFile);
?>

 

I put 'or die' after the delete function, but this is totally being ignored and the unlink function is being executed. Please help I want the script to stop if it cannot find anything in the database.

 

The problem is that's going to be valid SQL even if 0 rows are affected so the die is doing what it's supposed to do.  Why not check for the existence of the file prior to running the query?

Hi guys, the issue with my PHP script at the moment is I want it to die if the file cannot be found in the database, this is my current code:

 

<?php
$file = $_GET["id"];
$delid = $_GET["delid"];
include "my_sql.php";
mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die;
$myFile = "./images/$file";
unlink($myFile);
?>

 

Thanks, I will let everyone know how it goes :)

 

I put 'or die' after the delete function, but this is totally being ignored and the unlink function is being executed. Please help I want the script to stop if it cannot find anything in the database.

 

The problem is that's going to be valid SQL even if 0 rows are affected so the die is doing what it's supposed to do.  Why not check for the existence of the file prior to running the query?

die() will only work if there was a problem executing the query.  If nothing was found in the db, it returns false, but that's not a failed execute.  You need to wrap a condition around your unlink.  something like this:

 

<?php
$file = $_GET["id"];
$delid = $_GET["delid"];
include "my_sql.php";
mysql_query ("DELETE FROM uploadedimg WHERE name = '$file' AND killcode = '$delid'")or die;
ijf (mysql_affected_rows() > 0) {
   $myFile = "./images/$file";
   unlink($myFile);
}
?>

Figured it out myself, thanks guys heaps :)

 

<?php
$file = $_GET["id"];
$delid = $_GET["delid"];
include "my_sql.php";
$result = mysql_query("SELECT * FROM uploadedimg WHERE name='$file' AND killcode='$delid'");
while($row = mysql_fetch_array($result))
  {
mysql_query ("DELETE FROM uploadedimg WHERE `name` = '$file' AND `killcode` = '$delid' LIMIT 1")or die;
$myFile = "./images/$file";
unlink($myFile);
echo "Deleted!";
  } 
  echo "Failed";
?>

 

Happy Holidays ;)

Figured it out myself, thanks guys heaps :)

 

<?php
$file = $_GET["id"];
$delid = $_GET["delid"];
include "my_sql.php";
$result = mysql_query("SELECT * FROM uploadedimg WHERE name='$file' AND killcode='$delid'");
while($row = mysql_fetch_array($result))
  {
mysql_query ("DELETE FROM uploadedimg WHERE `name` = '$file' AND `killcode` = '$delid' LIMIT 1")or die;
$myFile = "./images/$file";
unlink($myFile);
echo "Deleted!";
  } 
  echo "Failed";
?>

 

Happy Holidays ;)

 

I would wager that code will echo both Deleted and Failed without a conditional in there somewhere.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.