Jump to content

[SOLVED] Die Function - Code Not Exiting


stormx

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.