Page 1 of 2
Multiple buttons single form
Posted: Fri Oct 10, 2008 2:54 am
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>
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 3:41 am
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.
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 4:52 am
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?
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 5:01 am
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?
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 8:32 am
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
)
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 9:01 am
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).
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 9:11 am
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
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));
}
?>
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 9:34 am
by mattcooper
Have you tried running the SQL query directly in your MySQL client?
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 9:42 am
by phpnewbieperson
mattcooper wrote:Have you tried running the SQL query directly in your MySQL client?
yep, worked fine. deleted the record.
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 9:48 am
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.
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 10:06 am
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

Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 10:12 am
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.
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 10:19 am
by phpnewbieperson
mattcooper wrote:And you're absolutely sure that this is being executed?
still no go.
I've put in a line:
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!!!
Re: Multiple buttons single form
Posted: Fri Oct 10, 2008 5:57 pm
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

Re: Multiple buttons single form
Posted: Sat Oct 11, 2008 2:03 am
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