Jump to content

Recommended Posts

Hi

I wonder can anyone help me with this problem. I know there's a lot of code here sorry. Everything with this code is fine but I wanted to set a session variable to ensure that a record is not inserted twice i.e. if session variables are set then the record has been inserted so skip the insertion code on refresh. So I've added a small piece of code to do this(the first line here if (!isset($_SESSION['set']) || !isset($_SESSION['check'])){ and the very last else statement at the bottom of the page. It works and doesn't insert the record again. However, it's doing the very first query after the 1st line (just to show the restaurant details)and it's not throwing back the contents of the final else statement. I just can't understand that. It should skip everything if the session variables are set. Any help would be great

 

 

if (!isset($_SESSION['set']) || !isset($_SESSION['check'])){

$query = "SELECT clientID, busName, strAddr1, townCity, county, bTelNum from clientdetails where clientID = '".$_SESSION['bookresid']."'";
$results = mysql_query($query, $link) or die("Sorry, cannot connect to database"); 
if(mysql_num_rows($results) > 0){ 
?><TABLE><TR><TD><H3>Restaurant details:</H3></TD></TR></TABLE><?
while($row = mysql_fetch_object($results)){ 
?><table><TR><TD><? echo $row->busName?></TD></TR><TR><TD><? echo $row->strAddr1?></TD></TR><TR><TD><? echo $row->townCity?></TD></TR><TR><TD><? echo $row->county?></TD></TR><TR><TD><? echo $row->TelNum?></TD></TR></TABLE><BR><?
}
}

//Carry out further validation of user form input

if (empty($_SESSION['firstname']) || empty($_SESSION['surname']) ||  !is_numeric($_SESSION['tele']) || empty($_SESSION['numcusts']) || empty($_SESSION['day']) || empty($_SESSION['mon'] ) || empty($_SESSION['year']))
{
//Determining which fields are empty so an asterix can be displayed on returned form
if (empty($_SESSION['firstname'])){ 
$dot1 = "<FONT color=red>*</FONT>"; 
}else{
$dot1 = ""; 
}
if (empty($_SESSION['surname'])) { 
$dot2 = "<FONT color=red>*</FONT>"; 
}else{
$dot2 = ""; 
}
if (!is_numeric($_SESSION['tele'])) { 
$dot3 = "<FONT color=red>*</FONT>"; 
}else{
$dot3 = ""; 
}
if (empty($_SESSION['numcusts'])) { 
$dot4 = "<FONT color=red>*</FONT>"; 
}else{
$dot4 = ""; 
}
if (empty($_SESSION['day'] ) || empty($_SESSION['mon']) || empty($_SESSION['year'])) {
$dot5 = "<FONT color=red>*</FONT>";
}else{
$dot5 = "";
}
echo $message;?>
<form id="form1" method="post" action="book.php">      
<table cellspacing=10><tr><TD><?echo $dot1?></TD><td><font color="#666666"><b>First Name:   </font></td>
<td><input type="text" name="firstname" size="15" value=<?echo $_SESSION['firstname']?>></td></tr>
<tr><TD><?echo $dot2?></TD><td><font color="#666666"><b>Surname:   </font></td>
<td><input type="text" name="surname" size="15" value=<?echo $_SESSION['surname']?>></td></tr>
<tr><TD><?echo $dot3?></TD><td><font color="#666666"><b>Telephone number:   </font></td>
<td><input type="text" name="tele" size="25" value=<?echo $_SESSION['tele']?>></td></tr>
<tr><TD><?echo $dot4?></TD><TD><font color="#666666"><b>How many customers?:  </font></TD>
<td><select name="numcusts">
    <option value="" selected="selected">Select number</option>
      <?php
for ($i=1;$i<=30;$i++){
?>
<option value="<?php echo $i; ?>" ><?php echo $i; ?></option>
<?php } ?>
    
   </select></td></tr> 
<TR><TD><?echo $dot5?></TD>
<TD><font color="#666666"><b>Day:</font></TD>
<TD><font color="#666666"><b>Month:</font></TD>
<TD><font color="#666666"><b>Year:</font></td>
<TD><font color="#666666"><b>Time:</font></TD></TR>   
<TR><TD><?echo $dot1?></TD><TD><select name="day">


    <option value="" selected="selected">--</option>
      <?php
for ($d=1;$d<=31;$d++){
if ($d < 10){
$d = "0".$d;}
?>
<option value="<?php echo $d; ?>" ><?php echo $d; ?></option>
<?php } ?>
  </select></td>  
<TD><select name="mon">
    <option value="" selected="selected">--</option>
      <?php
for ($m=1;$m<=12;$m++){
if ($m < 10){
$m = "0".$m;}

?>
<option value="<?php echo $m; ?>" ><?php echo $m; ?></option>
<?php } ?>
    
   </select></td>  


<TD><select name="year">
    <option value="" selected="selected">----</option>
    <?php
for ($y=2008;$y<=2020;$y++){
?>
<option value="<?php echo $y; ?>" ><?php echo $y; ?></option>
<?php } ?>
</select></td>
  
   <TD><select name="time">
       <option value="" selected="selected">----</option>
              <option value="" selected="selected">----</option>
       <option value="19:00:00" >19:00</option>
       <option value="19:15:00" >19:15</option>
       <option value="19:30:00" >19:30</option>
       <option value="19:45:00" >19:45</option>
       <option value="20:00:00" >20:00</option>
       <option value="20:15:00" >20:15</option>
       <option value="20:30:00" >20:30</option>
       <option value="20:45:00" >20:45</option>
       <option value="21:00:00" >21:00</option>
       <option value="21:15:00" >21:15</option>
       <option value="21:30:00" >21:30</option>
       <option value="21:45:00" >21:45</option>
       <option value="22:00:00" >22:00</option></select></TD></TR>


</TABLE><BR><BR><input id="inputsubmit1" type="submit" name="booking" value="Submit booking" /></form>
<?
}else{
//Determine what time the booking is for and amend sql accordingly
if ($_SESSION['time'] < '20:01:00'){
$sqladd = "and BookingTime between '18:59:00' and '20:01:00'";
}else{
$sqladd = "and BookingTime > '20:00:00'";
}
//Simply retrieving this restaurants seating capacity
$query = "select NumSeats FROM clientbooking where clientID = '".$_SESSION['bookresid']."' "; 
$results = mysql_query($query, $link) or die("Error performing query"); 
$date = $_SESSION['year'] . "-". $_SESSION['mon'] ."-". $_SESSION['day'];
if(mysql_num_rows($results) > 0){ 
while($row = mysql_fetch_object($results)){ 
$numseats = $row->NumSeats;

}
//Checking to see if there are any bookings for this date and time. Retrieving how many custs are booked in.
//If there are bookings, count them - if not insert the booking record and set a flag($check)
$query = "select ClientID, BookingNum, CustSName, NumPeople, BookingDate, BookingTime FROM userbookdetails where BookingDate =  '$date' and ClientID = '".$_SESSION['bookresid']."' $sqladd "; 
$results = mysql_query($query, $link) or die("Error performing query"); 
if(mysql_num_rows($results) > 0){ 
while($row = mysql_fetch_object($results)){ 
$numcount = $numcount + $row->NumPeople;
}
}
else{
$query = ("insert into userbookdetails ( ClientID, CustFname, CustSName, TelNum, 
NumPeople, BookingDate, BookingTime) VALUES ('".$_SESSION['bookresid']."', '".$_SESSION['firstname']."', 
'".$_SESSION['surname']."', '".$_SESSION['tele']."', '".$_SESSION['numcusts']."', '".$date."', '".$_SESSION['time']."')"); 

$results = mysql_query($query, $link) or die("First insert failure. Can't performing query"); 
$_SESSION['check'] = "set";
echo "<H3>Thank you for booking with us. We look forward to seeing you.</H3>";


echo ("<BR>");
echo "<H3>Your booking details are as follows:</H3>";
?><TABLE style="color: #408080;" ><TR><td>Name:  </TD><TD><?
echo ("</TD><TD>   </TD><TD>");
echo $_SESSION['firstname'] . " " . $_SESSION['surname'];
echo "</td></TR>";
echo  "<TR><TD>Number of customers:  </TD><TD>";
echo ("</TD><TD>   </TD><TD>");
echo $_SESSION['numcusts'];
echo "</TD></TR>";
echo "<TR><TD>Time of booking: </TD><TD>";
echo ("</TD><TD>   </TD><TD>");
echo $_SESSION['time'];
echo "</TD></TR>";
echo "<TR><TD>Date of booking: </TD><TD>";
echo ("</TD><TD>   </TD><TD>");
echo $date;
echo "</TD></TR></TABLE>";


}
//Deduct present bookings from total seating to find balance
$avail = ($numseats - $numcount);

//If the record was previously inserted then don't insert it again (isset function)
if (!isset($_SESSION['check'])){
//If there is available space for this booking insert it
if (($avail > $_SESSION['numcusts'])){
$query = ("insert into userbookdetails ( ClientID, CustFname, CustSName, TelNum, 
NumPeople, BookingDate, BookingTime) VALUES ('".$_SESSION['bookresid']."', '".$_SESSION['firstname']."', 
'".$_SESSION['surname']."', '".$_SESSION['tele']."', '".$_SESSION['numcusts']."', '".$date."', '".$_SESSION['time']."')"); 
$results = mysql_query($query, $link) or die("second insert failure. Can't performing query"); 
echo "<H3>Thank you for booking with us. We look forward to seeing you</H3>";


echo ("<BR>");
echo "<H3>Your booking details are as follows:</H3>";
?><TABLE style="color: #408080;" ><TR><td>Name:  </TD><TD><?
echo ("</TD><TD>   </TD><TD>");
echo $_SESSION['firstname'] . " " . $_SESSION['surname'];
echo "</td></TR>";
echo  "<TR><TD>Number of customers:  </TD><TD>";
echo ("</TD><TD>   </TD><TD>");
echo $_SESSION['numcusts'];
echo "</TD></TR>";
echo "<TR><TD>Time of booking: </TD><TD>";
echo ("</TD><TD>   </TD><TD>");
echo $_SESSION['time'];
echo "</TD></TR>";
echo "<TR><TD>Date of booking: </TD><TD>";
echo ("</TD><TD>   </TD><TD>");
echo $date;
echo "</TD></TR></TABLE>";
$_SESSION['set'] = "set";



}else{
echo "<h3>Sorry, we do not have available seating for this date and time</h3>";
echo $_SESSION['numcusts'];
}

}

}
else{
echo "This restaurant is not currently taking any bookings";
}


}


}else{
echo "Your booking has already been made";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/121848-solved-isset-session-variable-problem/
Share on other sites

The best way to stop multiple records being inserted on a page refresh is to reload the page after the insert query has run so:

 

// insert the record

mysql_query("");

// redirect the user

header("Location:page.php?booking=true");

 

Then you can check for the $_GET['booking'] and display the user success message

header() will only work providing that you haven't displayed ANY data at all on the page before. Including HTML.

 

Check if headers have been sent, if they have use either javascript or HTML to do the redirect.

 

if (!headers_sent()) {
  header("Location: page.php");
}
else {
  $redirect  = '<script type="text/javascript">window.location = "page.php";</script>';
  $redirect .= '<noscript><meta type="refresh" content="0;url=page.php" /></noscript>';
  echo $redirect;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.