uniqid() to stop a reload

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
jeaux
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 5:11 pm

uniqid() to stop a reload

Post by jeaux »

This is what I have.

MySQL table:

Code: Select all

CREATE TABLE IF NOT EXISTS `universitymenu` (
  `MenuItemID` int(11) NOT NULL auto_increment,
  `MenuCategoryID` int(11) NOT NULL,
  `Order` int(11) NOT NULL,
  `ItemName` varchar(100) NOT NULL,
  `ItemCost` decimal(10,2) NOT NULL,
  `ItemDescription` varchar(255) NOT NULL,
  PRIMARY KEY  (`MenuItemID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
 
--
-- Dumping data for table `universitymenu`
--
 
INSERT INTO `universitymenu` (`MenuItemID`, `MenuCategoryID`, `Order`, `ItemName`, `ItemCost`, `ItemDescription`) VALUES
(1, 3, 2, 'Ensalada de Pollo', 5.95, 'Grilled chicken breast over mixed greens, tossed in a balsamic vinaigrette.'),
(2, 3, 1, 'Alcachofas Estofadas', 5.95, 'Braised artichokes with serrano ham, sweet pea salad, walnut dressing, and manchego cheese.'),
(3, 3, 3, 'Plato de Jamon Serrano', 5.95, 'Plate of serrano ham with manchego and fresh fruit.'),
(4, 4, 1, 'Gambas al Ajillo', 7.95, 'Shrimp in lemon garlic olive oil with red chili peppers and fresh cilantro.');
 

The php:

Code: Select all

<?php
//connection data excluded 
  $conn = mysql_connect($dbhost, $dbuser, $dbpass)
            or die('Error connecting to MySQL.');
 
  mysql_select_db($dbname)
            or die('Error selecting database.');
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Delete menu item</title>
</head>
<body>
 
<?php
//User has hit the delete item button to confirm the deletion of the selected item
if (isset($_POST["ConfirmDelete"]))
                       {
                                $MenuItemID=(trim($_POST['MenuItemID']));
                                $sql="DELETE FROM `universitymenu` WHERE `MenuItemID` = '$MenuItemID'";
                                if (!mysql_query($sql,$conn))
                                   {
                                      die('Error: ' . mysql_error());
                                   }
                                else
                                   {
                                      echo "1 Item Deleted";
                                      mysql_close($conn);
                                   }
 
                      }
//User has selected the item for deletion and this is the confirmation of selection
        else if (isset($_POST["SubmitSelectedItem"]))
               {
                   $selecteditem=mysql_escape_string(trim($_POST['selecteditem']));
                   $SelectedItemQuery=mysql_query("SELECT * FROM `universitymenu` WHERE `MenuItemID` = '$selecteditem'");
?>
 
    <form action="<?php echo $PHP_SELF;?>" method="post">
        <fieldset>
        <legend>Delete menu item</legend>
        <p>Are you sure that you want to delete this item?</p>
        <p><input type="hidden" name="MenuItemID" value="
<?php
     while ($SelectedItemRow = mysql_fetch_array($SelectedItemQuery))
          {
             echo $SelectedItemRow['MenuItemID'];
?>
">
<?php
             echo $SelectedItemRow['ItemName'];
         }
?>
</p>
        <p><input class="submit" type="submit" name="ConfirmDelete" value="Delete item">
        </fieldset></p>
    </form>
<?php
//This is the start of the page without any previous action
                } else
                        {
                            $AllItems=mysql_query('SELECT * FROM universitymenu');
?>
 <p>Select menu item to delete.</p>
 <form name="editmenuitem" action="<?php echo $PHP_SELF;?>" method="POST">
<div align="left"><br>
<table width="90%" border="1">
<?php while ($AllItemsRow = mysql_fetch_array($AllItems)) { ?>
  <tr>
     <td width="5%"><input type="radio" name="selecteditem" value="
<?php echo $AllItemsRow['MenuItemID']; ?>
"></td>
     <td width="74%">
<?php echo $AllItemsRow['ItemName']; ?>
     </td>
     <td width="21%">$
<?php echo $AllItemsRow['ItemCost']; ?>
     </td>
  </tr>
  <tr>
     <td width="5%"></td>
     <td colspan="2">
<?php echo $AllItemsRow['ItemDescription']; ?>
     </td>
  </tr>
<?php } ?>
</table>
<p><input class="submit" type="submit" name="SubmitSelectedItem" value="Delete item">
</div>
</form>
<?php
             }
?>
</body>
</html>
 
This used to be three separate pages that I've condensed into one, and I know it's sloppy. Sorry I'm a noob and just trying to wrap my brain around this.

What I want to do is to use the uniqid() function to keep the user from reloading the page as they progress through the different submits. Can someone please post a script where they used uniqid() for that same purpose. Should I focus my studies on sessions or cookies or what?

Many thanks for any direction you could provide,
Joe
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: uniqid() to stop a reload

Post by josh »

If you need to set a stateful variable, that is a variable you can set to check which forms have already been completed, you should look into sessions, yes.
Post Reply