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!
I have listed code below. The problem I'm having is with the 'remove' section. When I try to remove an order, the order still appears on the page until I click on 'remove' a second time.
<?php
ob_start();
session_start();
$authorizedUsers = "visitor,admin";
$donotCheckaccess = "false";
$_SESSION['prevUrl']="orders.php";
if (isset($_SESSION['frm_username']))
{
showPage();
}
else
{
header("Location:login.php");
}
switch(@$_GET["action"])
{
case "remove_orders":
{
RemoveOrder($_GET["orderID"]);
}
}
function RemoveOrder($orderID)
{
// Uses an SQL delete statement to remove an item from
// the users cart
include ('connection/db.php');
mysql_query("DELETE FROM orders WHERE orderID = $orderID");
showPage();
}
function showPage()
{
error_reporting (E_ALL);
function showOrders()
{
include ('connection/db.php');
$orderQuery="SELECT * FROM orders WHERE userID='" . $_SESSION['userID'] . "'";
//Retrieve the item details of the cart
$result = mysql_query ($orderQuery) or die("ERROR, ERROR!".mysql_error());
$numRows = mysql_num_rows ($result);
if ($numRows == 0)
{
echo "<h4><align =",center,">You do not have any open orders at present.</h4><br />";
}
else
{
echo "
<table align=",center,">
<tr>
<td><h1>Your Shopping Cart</h1>
</td>
</tr>
<tr>
<td><h4>Order ID</h4></td>
<td><h4>Date Submitted</h4></td>
<td><h4>Date Shipped</h4></td>
<td><h4>Total Value of Order</h4></td>
<td><h4>Remove?</h4></td>
</tr>";
//Go through each of the items in the cart
while ($row=@mysql_fetch_array($result))
{
//Show the details of the item
echo "\n\t<td><h3>";
echo $row["orderID"];
echo "</h3></td>";
//Show the details of the item
echo "\n\t<td><h3>";
echo $row["dateSubmitted"];
echo "</h3></td>";
//Show the details of the item
echo "\n\t<td><h3>";
echo $row["dateShipped"];
echo "</h3></td>";
//Show the details of the item
echo "\n\t<td><h3>";
echo $row["total"];
echo "</h3></td>";
//Link to remove item - blank for now
echo "\n\t<td>";
echo "<a href=orders.php?action=remove_orders&orderID=",$row["orderID"],">Remove</a>";
echo "</td>";
echo "\n</tr>";
}
}
}
?>
If I refresh it, the data is gone - so its clearly working. I just can't figure out how to run this so it works... I can't seem to get the ordering right.
I don't think its a caching problem. I actually copied the code from another application I have that works properly. Here is the code from which it was copied:
<?php
switch(@$_GET["action"])
{
case "remove_item":
{
RemoveItem($_GET["piecenum"]);
break;
}
}
function RemoveItem($piecenum)
{
// Uses an SQL delete statement to remove an item from
// the users cart
include ('connection/db.php');
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND piecenum = $piecenum");
}
?>
This code doesn't require that I click twice. Very strange...
if (isset($_SESSION['frm_username']))
{
showPage();
}
else
{
header("Location:login.php");
}
Try moving that code below your switch() statement. The problem looks like you are first displaying the page then doing the delete, rather than doing the delete then displaying the page.