[SOLVED] Multiple Delete functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

[SOLVED] Multiple Delete functions

Post by Addos »

If I pass a URL parameter to this, the delete function works perfectly:

Code: Select all

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

if ((isset($_GET['ensembleID'])) && ($_GET['ensembleID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM conpositions_ensemble WHERE ensembleID=%s",
                       GetSQLValueString($_GET['ensembleID'], "int"));

  mysql_select_db($database_johnston, $johnston);
  $Result1 = mysql_query($deleteSQL, $johnston) or die(mysql_error());
}
?>
If I try to add another delete function (see below) then the script stops working in that it doesn’t delete but neither do I get an error.
Can anyone advise or show me where I’m going wrong? Can I not have more than one delete function on one page?
Thanks very much.
Brian

Code: Select all

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
if ((isset($_GET['electro_ID'])) && ($_GET['electro_ID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM compositions_electro WHERE electro_ID=%s",
                       GetSQLValueString($_GET['electro_ID'], "int"));
					   }
 elseif ((isset($_GET['ensembleID'])) && ($_GET['ensembleID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM conpositions_ensemble WHERE ensembleID=%s",
                       GetSQLValueString($_GET['ensembleID'], "int"));
					   }
 elseif  ((isset($_GET['programme_ID'])) && ($_GET['programme_ID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM programme_notes WHERE programme_ID=%s",
  					   GetSQLValueString($_GET['programme_ID'], "int"));
			   				   
					   					   
  mysql_select_db($database_johnston, $johnston);
  $Result1 = mysql_query($deleteSQL, $johnston) or die(mysql_error());
}
Last edited by Addos on Sun Apr 17, 2005 7:46 am, edited 1 time in total.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

Not sure if this is the only problem, but maybe it will help..

You have:

Code: Select all

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  // [...]

  switch ($theType) {

    // [...]    

    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;

    // [...]

  }
  return $theValue;
}
 
$deleteSQL = sprintf("DELETE FROM conpositions_ensemble WHERE ensembleID=%s", GetSQLValueString($_GET['ensembleID'], "int"));
Now, what I noticed is that your function is returning an Integer type, and you are passing it to sprintf() as "%s" which is for string types..

But, you have solved your own problem already, here:

Code: Select all

//[...]

    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;

//[...]
You concatenated the value with a string making the return value a string, which I think is what you want to do for the "int" types, even if you dont surround them in single-quotes..
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i dunno if this is worth mentioning, but i'm going to anyways...

this

Code: Select all

elseif ((isset($_GET['ensembleID'])) && ($_GET['ensembleID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM conpositions_ensemble WHERE ensembleID=%s",GetSQLValueString($_GET['ensembleID'], "int"));
catches my eyes ;). cuz isn't this suposed to be compositions instead of conpositions?
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Hi,
Thanks for you reply folks,

The word compositions is ok in that it is mispelt in my database too and I just didn’t need to correct it for the sake of spelling.

What is puzzling me is that as I was saying in my first post why does any of the delete functions eg

Code: Select all

elseif  ((isset($_GET['programme_ID'])) && ($_GET['programme_ID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM programme_notes WHERE programme_ID=%s",
                         GetSQLValueString($_GET['programme_ID'], "int"));
....work once they are not all combined together within the code. Should I just be aiming for a single delete page for every URL parameter I pass?

Thanks again
Brian
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

You are still passing an integer to sprintf() in the place of a string.. ( "%s" )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php does implicit conversion from string to integer and back for you.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

Indeed, but what about when you pass a value to sprintf() using a control sequence like %s?? Aren't you explicitly telling PHP to treat the value as a string (Like in C/C++)?
Or does PHP implicitly convert before even passing the value to sprintf()? If so, then how does PHP know you want a string value before ever parsing the format string of sprintf() and finding a %s control sequence?
I mean, if PHP does all implicit conversions, then what is the point of even using control sequences like %s, %d, etc..?

In the end, he is still passing an integer into a %s control sequence..

(Not trying to argue your point feyd, but I just need some clarification on this I guess, since it sounds like PHP handles control sequences different from C/C++?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not test it out? :) You'll find out your answers, and likely more..

just remember this: every "function" in php isn't a direct mapping to the C source. There's a lot of engine work you don't see.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

feyd wrote: just remember this: every "function" in php isn't a direct mapping to the C source. There's a lot of engine work you don't see.
:oops: Ahh.. makes sense now, nevermind me! Still too used to C/C++, it *appeared* as though functions like sprintf() were simple wrappers of the C/C++ versions.. (pardon my ignorance :P)

Ahh, but I think I just noticed a logic problem in your code Addos, you basically have:

Code: Select all

if ((isset($_GET['electro_ID'])) && ($_GET['electro_ID'] != ""))
{
  $deleteSQL = "blah blah";
}
 elseif ((isset($_GET['ensembleID'])) && ($_GET['ensembleID'] != ""))
{
  $deleteSQL = "blah blah";
}
 elseif  ((isset($_GET['programme_ID'])) && ($_GET['programme_ID'] != ""))
{
  $deleteSQL = "blah blah";
                                                                                
  mysql_select_db($database_johnston, $johnston);
  $Result1 = mysql_query($deleteSQL, $johnston) or die(mysql_error());
}
Notice that the mysql_query() function is within the Last IF block?? It should probably be outside of it, otherwise the first two conditionals simply create a variable (namely $deleteSQL) but then never do anything with it...

Try moving it to look something like:

Code: Select all

if ((isset($_GET['electro_ID'])) && ($_GET['electro_ID'] != ""))
{
  $deleteSQL = "blah blah";
}
 elseif ((isset($_GET['ensembleID'])) && ($_GET['ensembleID'] != ""))
{
  $deleteSQL = "blah blah";
}
 elseif  ((isset($_GET['programme_ID'])) && ($_GET['programme_ID'] != ""))
{
  $deleteSQL = "blah blah";                                                                                
}

  mysql_select_db($database_johnston, $johnston);
  $Result1 = mysql_query($deleteSQL, $johnston) or die(mysql_error());
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thank you all so much. A simple position of } can cause so much problems for a beginner like me. Thanks for taking the time and effort to help me.

Brian
Post Reply