Multiple buttons single form

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

phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Multiple buttons single form

Post by phpnewbieperson »

Hey,

I'm trying to use two buttons on a single form. One for updating, one for deleting. The update function works fine, but the delete button just posts back. I know the Delete value is in the form post variables when the delete button is clicked as I can write it to the page after posting back....

Any ideas why the delete function isn't working?

The crux of the code is:

Code: Select all

if (isset($_POST["Update"])) {
            $updateSQL = sprintf("UPDATE tblreservations SET AgentID=%s WHERE ReservationID=%s",
           GetSQLValueString($_POST['AgentID'], "int"),
           GetSQLValueString($_POST['ReservationID'], "int"));
        
            mysql_select_db($database_connDB, $connDB);
            $Result1 = mysql_query($updateSQL, $connDB) or die(mysql_error());
        
            $updateGoTo = "bookings_edit.php?m=edited";
            if (isset($_SERVER['QUERY_STRING'])) {
              $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
              $updateGoTo .= $_SERVER['QUERY_STRING'];
    } elseif (isset($_POST["Delete"])) {
            $updateSQL = sprintf("DELETE FROM tblreservations WHERE tblreservations.ReservationID=%s LIMIT 1",
            GetSQLValueString($_POST['ReservationID'], "int"));
        
            mysql_select_db($database_connDB, $connDB);
            $Result1 = mysql_query($updateSQL, $connDB) or die(mysql_error());
        
            $updateGoTo = "bookings_edit.php?m=deleted";
            if (isset($_SERVER['QUERY_STRING'])) {
              $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
              $updateGoTo .= $_SERVER['QUERY_STRING'];
              }
    }
    header(sprintf("Location: %s", $updateGoTo));
}
and the form is basically this:

Code: Select all

<form action="bookings_edit.php" method="post" name="form1" id="form1">
form fields in here
<input type="Submit" name="Update" value="Update" /> <input type="Submit" name="Delete" value="Delete" onclick="if (! confirm('Are you sure?')) { return false; }" />
 <input type="hidden" name="ReservationID" value="<?php echo $row_rsBookingToEdit['ReservationID']; ?>" />
</form>
 
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Multiple buttons single form

Post by mattcooper »

Have you tried printing the SQL query to debug? Please try that and if it doesn't work print all of your posted inputs and post here.
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

I have just printed the SQL query, problem is it's being set for the update command, but is not being updated or reset for the delete command. So there must be something wrong with my php syntax for setting the query. Any ideas?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Multiple buttons single form

Post by mattcooper »

phpnewbieperson wrote:I have just printed the SQL query, problem is it's being set for the update command, but is not being updated or reset for the delete command. So there must be something wrong with my php syntax for setting the query. Any ideas?
I asked for the output of your script... can you post more information to help me debug this?
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

I am not sure what you mean? I've posted pretty much the entire code of the page.

I've printed to the screen all post values. When I click update, all works fine, the db is updated. When I click delete, nothing happens (despite the system outputting all the post values). These are the post values I get when I click delete:

Code: Select all

Array
(
    [AgentID] => 1
    [Delete] => Delete
    [ReservationID] => 18
)
 
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Multiple buttons single form

Post by mattcooper »

And so the resultant SQL string is definitely this?

Code: Select all

 
DELETE FROM tblreservations WHERE tblreservations.ReservationID=18 LIMIT 1
 
That's kinda what I was getting at. If this is the case, I think you'll need to take a look at things like permissions on this table, assuming that this particular row of data actually exists (i.e, all SQL conditions are being met - clearly the PHP conditions are because you're able to print the values passed to that part of your script).
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

mattcooper wrote:And so the resultant SQL string is definitely this?
yep.
mattcooper wrote:take a look at things like permissions on this table
so far, I'm only testing locally, running as root user, so I can only assume delete permissions are fine.
mattcooper wrote:assuming that this particular row of data actually exists (i.e, all SQL conditions are being met - clearly the PHP conditions are because you're able to print the values passed to that part of your script).
yep, spot on. It's frustrating the hell out of me as it should be so simple and straight forward :banghead:

I've updated the code slightly, but still getting nothing happening on clicking delete. As follows:

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
other textboxes here.
<input type="Submit" name="Action" value="Update" /> <input type="Submit" name="Action" value="Delete" onclick="if (! confirm('Are you sure?')) { return false; }" />
<input type="hidden" name="ReservationID" value="<?php echo $row_rsBookingToEdit['ReservationID']; ?>" />
</form>

Code: Select all

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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;
}
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST['Action'])) && ($_POST['Action'] == "Update")) {
            $updateSQL = sprintf("UPDATE tblreservations SET AgentID=%s WHERE ReservationID=%s",
           GetSQLValueString($_POST['AgentID'], "int"),
           GetSQLValueString($_POST['ReservationID'], "int"));
        
            mysql_select_db($database_connDB, $connDB);
            $Result1 = mysql_query($updateSQL, $connDB) or die(mysql_error());
            
            $updateGoTo = "bookings_edit.php?m=edited";
            if (isset($_SERVER['QUERY_STRING'])) {
              $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
              $updateGoTo .= $_SERVER['QUERY_STRING'];
    } elseif ((isset($_POST['Action'])) && ($_POST['Action'] == "Delete")) {
            $updateSQL = sprintf("DELETE FROM tblreservations WHERE ReservationID=%s LIMIT 1",
            GetSQLValueString($_POST['ReservationID'], "int"));
        
            mysql_select_db($database_connDB, $connDB);
            $Result1 = mysql_query($updateSQL, $connDB) or die(mysql_error());
            
            $updateGoTo = "bookings_edit.php?m=deleted";
            if (isset($_SERVER['QUERY_STRING'])) {
              $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
              $updateGoTo .= $_SERVER['QUERY_STRING'];
              }
    }
    header(sprintf("Location: %s", $updateGoTo));
}
?>
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Multiple buttons single form

Post by mattcooper »

Have you tried running the SQL query directly in your MySQL client?
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

mattcooper wrote:Have you tried running the SQL query directly in your MySQL client?
yep, worked fine. deleted the record.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Multiple buttons single form

Post by mattcooper »

change the code in the "elseif" part of your control to:

Code: Select all

 
die("DELETE FROM tblreservations WHERE ReservationID=$_POST['ReservationID'] LIMIT 1");
 
and post the output back here.
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

mattcooper wrote:change the code in the "elseif" part of your control to:

Code: Select all

 
die("DELETE FROM tblreservations WHERE ReservationID=$_POST['ReservationID'] LIMIT 1");
 
and post the output back here.
nothing happened. output is:

DELETE FROM tblreservations WHERE ReservationID='15' LIMIT 1

NB: ID is 15 now as 18 was deleted manually :)
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Multiple buttons single form

Post by mattcooper »

And you're absolutely sure that this is being executed, or at least passed to mysql_query()?

Try

Code: Select all

 
if (mysql_query('yourQueryString')) { echo "tried to run the query"; } else { echo "didn't try to run the query"; }
 
I'm gonna give up if this doesn't work, but I still think it's something that you're not testing for properly.
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

mattcooper wrote:And you're absolutely sure that this is being executed?
still no go.

I've put in a line:

Code: Select all

$_SESSION['SQL'] = $updateSQL;
just after

Code: Select all

$updateSQL = "DELETE FROM tblreservations WHERE ReservationID=$_POST['ReservationID'] LIMIT 1"
I've got an echo on the page to write the session variable 'SQL' after the delete button is hit.....it's empty!!!
phpnewbieperson
Forum Newbie
Posts: 22
Joined: Wed Mar 26, 2008 8:25 am

Re: Multiple buttons single form

Post by phpnewbieperson »

mattcooper wrote: Try

Code: Select all

 
if (mysql_query('yourQueryString')) { echo "tried to run the query"; } else { echo "didn't try to run the query"; }
 
I placed that on the page, and yep, that query executed fine, and the record was deleted. But that query isn't being executed when its in that if else statement???

I have tested everything I can, and am at a total loss as to why this trivial task is not working :cry:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multiple buttons single form

Post by pickle »

Why are you testing for $_POST['Action']? There's no variable named 'Action' in your form, and 'Action' isn't being POSTed. Change your form so the name of the submit elements is 'Action' & it should work.

Also, when posting PHP or HTML code, please use

Code: Select all

&

Code: Select all

tags respectively.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply