Copy link to clipboard
Copied
I have a delete routine that displays contents of a record. Two buttons - CANCEL & CONFIRM DELETE. The cancel allows me to send a parameter with the header("Location ...) and all is well. When I choose confirm delete, the record is deleted, but the value of the parameter is cleared. I have set the parameter to a value in the record WHEN THE RECORD IS READ. Nothing in the delete routine changes the independent value, but it gets cleared when the record is deleted. Is there any way to preserve a value in the record before it's deleted? I have a table with a large # of records. After deleting, I want to return to the display of the selected subset.
I'm setting a variable $CycleYear = $r_Cycle_Year which is set when the field r_Cycle_Year is bound to $r_Cycle_Year. Why does $CycleYear get cleared when the record is deleted? I understand that all record values are undefined after deletion, but why can't I preserve a value for use as a parameter?
Thanks for any advice!
To me, it sounds like the delete fuction refreshes the page or leads to another page. In either case , the variable will be lost. There are a number of ways to preserve the value of the variable. Have a look at https://www.w3schools.com/html/html5_webstorage.asp. Another way is to use a session variable.
Copy link to clipboard
Copied
To me, it sounds like the delete fuction refreshes the page or leads to another page. In either case , the variable will be lost. There are a number of ways to preserve the value of the variable. Have a look at https://www.w3schools.com/html/html5_webstorage.asp. Another way is to use a session variable.
Copy link to clipboard
Copied
After the delete, I'm calling
header("Location: DisplayRCL.php?CycleYear=$CycleYear);
When the Cancel button is selected, this command is sent successfully.
When the Confirm Delete button is selected, $CycleYear is null.
Why?
Copy link to clipboard
Copied
header("Location: DisplayRCL.php?CycleYear=$CycleYear);
This creates a new HTTP request and discards the old. See my solution above.
Copy link to clipboard
Copied
If the php 'header' redirect function exists on the same page as the 'delete record query' then you should be able to send the deleted record variable by url parameter to another page, regardless of IF the record has been deleted or not.
Below is a simple example using mysqli, deleting the record, redirecting to 'delete_success.php' whilst sending the 'record_id', then getting and echoing the 'record_id' on the 'delete_success.php' page
<?php
$record_id = $_GET['record_id'];
$conn->query("DELETE FROM tableName WHERE record_id='$record_id'");
header("Location: delete_success.php?record_id=$record_id");
?>
//Getting the 'record_id' on the delete_success.php page
<?php
$record_id = $_GET['record_id'];
echo $record_id;
?>
Could it be the way in which you have your 'Cancel' and 'Confirm Delete' buttons set up. If they go back to the same page then you will lose the 'record_id' variable, like Bens says, UNLESS you have included the 'record_id' variable in the 'Confirm Delete' button link. You can also send an 'action' via the link:
<a href="delete_success.php?record_id=<?php echo $record_id; ?>?action=delete">Confirn Delete</a>
Get the 'action' and do something according to if the 'action' is 'delete' of not:
<?php
$action = $_GET['action'];
if($action === "delete" {
$record_id = $_GET['record_id'];
$conn->query("DELETE FROM tableName WHERE record_id='$record_id'");
header("Location: delete_success.php?record_id=$record_id");
}
else {
DO SOMETHING ELSE
}
?>
Copy link to clipboard
Copied
Thanks! I didn't know that if the 2 buttons went back to the same page that I would lose the variable. How do I include the variable in the button link? I lose the variable only in the Delete button, NOT in the Cancel button. I'm very confused! I appreciate your help!
Copy link to clipboard
Copied
Why not just style the 'Confirm Delete' button just like you have with your Cancel button?
You can pass what variables you like via the link url, ie the 'action', 'deletekey' and CycleYear
<a href="DisplayRCL.php?CycleYear=<?php echo $CycleYear; ?>&action=delete&deletekey=<?php echo $deletekey; ?>" ><span class="GreenButton"> CONFIRM DELETE </span></a>
Then use the below query:
$action = $_GET['action'];
if ($action === "delete") {
$CycleYear = $_GET['CycleYear'];
$deleteSQL = "DELETE FROM RCL_selections WHERE r_key_1=?";
$stmt=$sainttim->prepare($deleteSQL);
$stmt->execute(array($_GET['deletekey']));
$deleted = $stmt->rowCount();
if (!$deleted)
{
$error = 'There was a problem deleting the record.';
}
else {
header("Location: DisplayRCL.php?CycleYear=$CycleYear");
}
}
Copy link to clipboard
Copied
I used your suggestion. It took me back to the right display, but didn't delete the record! But thanks. Solved the problem using a session variable.
Copy link to clipboard
Copied
I used your suggestion. It took me back to the right display, but didn't delete the record! But thanks. Solved the problem using a session variable.
I guess your code was set up slightly wrong somehow if the record wasn't deleted.......I dont use PDO, which I believe you are using, so I may have provided the example incorrectly.........however a session variable will do the same job of 'preserving' the variable, so whatever works for you.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I'll try once more!
if (isset($_POST['delete']))
{
$deleteSQL = "DELETE FROM RCL_selections WHERE r_key_1=?";
$stmt=$sainttim->prepare($deleteSQL);
$stmt->execute(array($_POST['deletekey']));
$deleted = $stmt->rowCount();
if (!$deleted)
{
$error = 'There was a problem deleting the record.';
}
else {
header("Location: DisplayRCL.php?CycleYear=$CycleYear");
exit;
}
}
<tr>
<td align="right"><a href="DisplayRCL.php?CycleYear=<?php echo $CycleYear; ?>" ><span class="RedButton"> CANCEL </span></a></td>
<td align="left"><input name="delete" id="delete" type="submit" class="GreenButton" value="Confirm Delete" /></td>
</tr>
Record is deleted. If Cancel is selected, the $CycleYear value is sent & the display routine shows the proper records.
If Delete, $CycleYear value is null and the display routine shows no records.
HELP!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks! I used a session variable to solve the problem. I didn't use the webstorage, but I used your concept and appreciate the advice.