Problem with session!

Links for php scripts

Moderators: gesf, Michalio

Post Reply
Rocky1948
New php-forum User
New php-forum User
Posts: 8
Joined: Tue Aug 16, 2022 12:00 pm

I have some code that passes some variables using sessions.
The program I have written allows the user to choose 2 parameters which I have called event & mood and these are posted ($_POST) and used in the next file to create a query based on these parameters. Everything works OK here, then the user chooses a verse and submits the choice. the next file is a form that allows the user to choose several parameters, size font etc and when this has been done the user submits the form and a file that prepares the output for a file fpdf which is a php programable PDF writer.
Everything seems OK and a PDF is produced, but it obviously failed at some stage because it writes 'You have selected an invalid Event', which is a statement posted if an invalid evet is chosen. The site has been developed using Site Designer by Coffeecup software.
There are 3 files- vinput.php, vquery.php and varInput.
vinput.php

Code: Select all

<!DOCTYPE html>
<html lang="en-GB">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta name="generator" content="RSD 5.0.3519">
  <meta name="description" content="Created by Inger Eik, Dec. 2021">
  <title>Verse Input</title>
  <link rel="stylesheet" href="css/bootstrap4.min.css">
  <link rel="stylesheet" href="css/wireframe-theme.min.css">
  <script>document.createElement( "picture" );</script>
  <script class="picturefill" async="async" src="js/picturefill.min.js"></script>
  <link rel="stylesheet" href="css/main.css">
    <style>
    /* Interaction Elements */

a.link-button.button-link-1 {
  margin-right: 5px;
  margin-bottom: 5px;
  margin-left: 5px;
  padding-top: 4px;
  width: 120px;
  height: auto;
  border: .0625rem solid rgba(255, 255, 255, .5);
  background-color: #0f0e14;
  color: #fff;
  text-align: center;
  text-decoration: none;
  line-height: 22px;
}

select.select.dd-fc {
  margin-right: 5px;
  margin-bottom: 5px;
  margin-left: 5px;
  padding-top: 5px;
  padding-bottom: 3px;
  width: 120px;
  border-color: rgba(255, 255, 255, .5);
  background-color: #0f0e14;
  color: #fff;
  opacity: .98;
}

.container-grid.dropdown-menubar {
  position: absolute;
  top: auto;
  bottom: 0;
  display: -webkit-box;
  display: -webkit-flex;
  display:    -moz-box;
  display: -ms-flexbox;
  display:         flex;
  -ms-flex-pack: center;
  margin-right: 0;
  margin-left: 0;
  padding-top: 10px;
  padding-bottom: 5px;
  max-width: 1200px;
  width: 100%;
  background-color: #0f0e14;

  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
          justify-content: center;
  -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
          flex-wrap: wrap;
}

@media screen and (min-width: 75rem) {
  /* Interaction Elements */

  a.link-button.button-link-1 {
    height: auto;
  }
}
option:first-child {
display:none;
  }


    </style>
</head>

<body>
  <div class="outer-limit container-grid">
    <div class="header container-grid">
      <div class="header-inner container-grid"></div>
      <div class="dropdown-menubar container-grid">
        <?php include("inc/menu.php"); ?>
      </div>
    </div>
    <h1 class="heading-5">1066 Card Craft</h1>
    <div class="content container-grid">
      <div class="aside container-grid">
        <div class="responsive-picture">
	 <picture>
            <img alt="Placeholder Picture" width="485" height="477" src="./img/writing.jpg" loading="lazy">
          </picture>

        <?php   include("inc/aside-verse.php");   ?>
</div>      
</div>
      <div class="main container-grid">
        <?php
     

include ("cons/connect_Verses4Cards.php");
$conn = get_db_conn_verse();
//Function to build select options based on passed array
function buildSelectOptions($name, array $options, $current=null){
	$htmlString  = "<select name='{$name}' id='{$name}'>\n";
	
	foreach($options as $value=>$option){
		$htmlString .= "\t<option value='{$value}'";
		if($value == $current){
		$htmlString .= " selected";
		}
		$htmlString .= ">{$option}</option>\n";
		}
		$htmlString .= "</select>\n";
		return $htmlString;
		}
		
function getEvents($conn){
		$qry = "SELECT id ,Event_Type FROM Events ORDER BY id";
		$sql = mysqli_query($conn, $qry)or die(mysqli_error($conn));
		if(mysqli_num_rows($sql) < 1){
		return array();
		}
		while($res1 = mysqli_fetch_array($sql)){
		$ret1[$res1['id']] = $res1['Event_Type'];
		
		}
		
		return $ret1;
		}
		
function getMoods($conn){
		$qry = "SELECT id ,Event_Sub_Type FROM Event_Sub ORDER BY id";
		$sql = mysqli_query($conn, $qry)or die(mysqli_error($conn));
		if(mysqli_num_rows($sql) < 1){
		return array();
		}
		while($res2 = mysqli_fetch_array($sql)){
		$ret2[$res2['id']] = $res2['Event_Sub_Type'];
		}
		
		return $ret2;
		}
		
?>
 
      
<h3>Choose Your Verse Options</h3>
<br></br>
<!-- Start of FORM -->
        <form action="vquery.php" method="post">


          <?php echo buildSelectOptions('event', getEvents($conn), 1);?>



          <?php echo buildSelectOptions('mood', getMoods($conn), 1);?>

          <input type="submit" value="submit" />
        </form>
        <!-- End of FORM -->
       
  </div>
  </div>
    <div class="footer container-grid">
      

      <?php   include("inc/footer.php");   ?>
    </div>
  </div>
  <script src="js/jquery.min.js"></script>
  <script src="js/outofview.js"></script>
  <script src="js/popper.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
</body>

</html>
vquery.php

Code: Select all

<!DOCTYPE html>
<html lang="en-GB">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta name="generator" content="RSD 5.0.3519">
  <meta name="description" content="Created by Inger Eik, Dec. 2021">
  <title>Verse Query</title>
  <link rel="stylesheet" href="css/bootstrap4.min.css">
  <link rel="stylesheet" href="css/wireframe-theme.min.css">
  <script>document.createElement( "picture" );</script>
  <script class="picturefill" async="async" src="js/picturefill.min.js"></script>
  <link rel="stylesheet" href="css/main.css">
   <?php
include_once("cons/connect_Verses4Cards.php");
$conn=get_db_conn_verse();

$display_block ="";
//check for required info from the query string


//verify the Event exists
$verify_Event_sql = "SELECT ID, Event_Type FROM Events WHERE ID = '".$_POST["event"]."'";
$verify_Event_res =  mysqli_query($conn, $verify_Event_sql) or die(mysqli_error($conn));



if (mysqli_num_rows($verify_Event_res) < 1) {
	//this Event does not exist
	$display_block = "<p><em>You have selected an invalid Event.<br/>
	Please try again.</em></p>";

} else {
//get the Event ID
	while ($Event_info = mysqli_fetch_array($verify_Event_res)) {
		$Event_ID = stripslashes($Event_info['ID']);
		$Event_Name = ($Event_info['Event_Type']);
	}

$verify_Mood_sql = "SELECT ID, Event_Sub_Type FROM Event_Sub WHERE ID = '".$_POST["mood"]."'";
$verify_Mood_res =  mysqli_query($conn, $verify_Mood_sql) or die(mysqli_error($conn));

if (mysqli_num_rows($verify_Mood_res) < 1) {
	//this Event does not exist
	$display_block = "<p><em>You have selected an invalid Mood.<br/>
	Please try again.</em></p>";
}

	while($Mood_info = mysqli_fetch_array($verify_Mood_res)) {
		$Mood_ID = ($Mood_info['ID']);
		$Mood_Name = ($Mood_info['Event_Sub_Type']);
	}
	//gather the Events
	$get_Event_sql  = "SELECT verses.ID AS versesID, verses.Verse, verses.Sub_Type, verses.Event, Events.ID AS eventsID, Events.Event_Type, Event_Sub.ID AS event_SubID, Event_Sub.Event_Sub_Type
	FROM verses
	LEFT JOIN Events
	ON verses.Event = Events.ID
	LEFT JOIN Event_Sub
	ON verses.Sub_Type = Event_Sub.ID
	WHERE verses.Event = '".$_POST["event"]."' And verses.Sub_Type = '".$_POST["mood"]."'
	ORDER BY verses.ID ASC";


	$get_Event_res = mysqli_query($conn, $get_Event_sql) or die(mysqli_error($conn));

	//create the display string
	$display_block .= "

<table width=\"90%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"white\" >
	<tr>
	<th>ID</th>
	<th>VERSE</th>
	<th>MOOD/SUB TYPE</th>
	<th>CHOOSE</th>
	</tr>";	

	while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
		$Verse_id = $Verse_info['versesID'];
		$Verse_text = nl2br(stripslashes($Verse_info['Verse']));
		$Mood_info = $Verse_info['Event_Sub_Type'];
		$VID = $Verse_id;
		//add to display
$display_block .= "
<tr>
<td width=\"1%\" valign=\"top\">".$Verse_id."<br/></td>
<td width=\"55%\" valign=\"top\">".$Verse_text."<br/></td>
<td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
<td width=\"35%\" valign=\"middle\">
<form METHOD=\"POST\" ACTION=\"varInput.php\">
<input type=\"Radio\" name=\"VID\" value=$VID ></td>
</tr>";
}

$display_block.="<input type=submit value=Submit>
</form>";


//free results
	mysqli_free_result($get_Event_res);
	mysqli_free_result($verify_Event_res);	
//close up the table
$display_block .="</table>";	
}
?>
 <style>
    /* Interaction Elements */

a.link-button.button-link-1 {
  margin-right: 5px;
  margin-bottom: 5px;
  margin-left: 5px;
  padding-top: 4px;
  width: 120px;
  height: auto;
  border: .0625rem solid rgba(255, 255, 255, .5);
  background-color: #0f0e14;
  color: #fff;
  text-align: center;
  text-decoration: none;
  line-height: 22px;
}

select.select.dd-fc {
  margin-right: 5px;
  margin-bottom: 5px;
  margin-left: 5px;
  padding-top: 5px;
  padding-bottom: 3px;
  width: 120px;
  border-color: rgba(255, 255, 255, .5);
  background-color: #0f0e14;
  color: #fff;
  opacity: .98;
}

.container-grid.dropdown-menubar {
  position: absolute;
  top: auto;
  bottom: 0;
  display: -webkit-box;
  display: -webkit-flex;
  display:    -moz-box;
  display: -ms-flexbox;
  display:         flex;
  -ms-flex-pack: center;
  margin-right: 0;
  margin-left: 0;
  padding-top: 10px;
  padding-bottom: 5px;
  max-width: 1200px;
  width: 100%;
  background-color: #0f0e14;

  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
          justify-content: center;
  -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
          flex-wrap: wrap;
}

@media screen and (min-width: 75rem) {
  /* Interaction Elements */

  a.link-button.button-link-1 {
    height: auto;
  }
}
option:first-child {
display:none;
  }


    </style>
</head>

<body>
  <div class="outer-limit container-grid">
    <div class="header container-grid">
      <div class="header-inner container-grid"></div>
      <div class="dropdown-menubar container-grid">
        <?php include("inc/menu.php"); ?>
      </div>
    </div>
    <h1 class="heading-5">1066 Card Craft</h1>
    <div class="content container-grid">
      <div class="aside container-grid">
       <div class="responsive-picture">
       <picture>
            <img alt="Placeholder Picture" width="485" height="477" src="./img/writing.jpg" loading="lazy">
          </picture>
    
        <?php   include("inc/aside-verse.php");   ?>
	</div>
      </div>
      <div class="main container-grid">
        <h2 class="heading-2">Your Verse Query</h2>
        
        <main class="container-grid">
          <?php if(isset($display_block)){
            echo $display_block;}?>
        </main>
      </div>
    </div>
    <div class="footer container-grid">
      <?php   include("inc/footer.php");   ?>
    </div>
  </div>
  <script src="js/jquery.min.js"></script>
  <script src="js/outofview.js"></script>
  <script src="js/popper.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
</body>

</html>
varInput.php

Code: Select all

<!DOCTYPE html>
<html lang="en-GB">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta name="generator" content="RSD 5.0.3528">
  <meta name="description" content="Created by Inger Eik, Dec. 2021">
  <title>Input Page</title>
  <link rel="stylesheet" href="css/bootstrap4.min.css">
  <link rel="stylesheet" href="css/wireframe-theme.min.css">
  <script>document.createElement( "picture" );</script>
  <script class="picturefill" async="async" src="js/picturefill.min.js"></script>
  <link rel="stylesheet" href="css/main.css">
    <style>
    /* Interaction Elements */

a.link-button.button-link-1 {
  margin-right: 5px;
  margin-bottom: 5px;
  margin-left: 5px;
  padding-top: 4px;
  width: 120px;
  height: auto;
  border: .0625rem solid rgba(255, 255, 255, .5);
  background-color: #0f0e14;
  color: #fff;
  text-align: center;
  text-decoration: none;
  line-height: 22px;
}

select.select.dd-fc {
  margin-right: 5px;
  margin-bottom: 5px;
  margin-left: 5px;
  padding-top: 5px;
  padding-bottom: 3px;
  width: 120px;
  border-color: rgba(255, 255, 255, .5);
  background-color: #0f0e14;
  color: #fff;
  opacity: .98;
}

.container-grid.dropdown-menubar {
  position: absolute;
  top: auto;
  bottom: 0;
  display: -webkit-box;
  display: -webkit-flex;
  display:    -moz-box;
  display: -ms-flexbox;
  display:         flex;
  -ms-flex-pack: center;
  margin-right: 0;
  margin-left: 0;
  padding-top: 10px;
  padding-bottom: 5px;
  max-width: 1200px;
  width: 100%;
  background-color: #0f0e14;

  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
          justify-content: center;
  -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
          flex-wrap: wrap;
}

@media screen and (min-width: 75rem) {
  /* Interaction Elements */

  a.link-button.button-link-1 {
    height: auto;
  }
}
option:first-child {
display:none;
  }


    </style>
</head>

<body>
  <div class="outer-limit container-grid">
    <div class="header container-grid">
      <div class="header-inner container-grid"></div>
      <div class="dropdown-menubar container-grid">
        <?php include("inc/menu.php"); ?>
      </div>
    </div>
    <h1 class="heading-5">1066 Cards 4 U</h1>
    <div class="content container-grid">
      <div class="aside container-grid">
        <?php   include("inc/aside.php");   ?>
      </div>
      <div class="main container-grid">
        <!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="generator" content="RSD 5.0.3519">
  <title>VInput1</title>
  <link rel="stylesheet" href="css/bootstrap4.min.css">
  <link rel="stylesheet" href="css/wireframe-theme.min.css">
  <script>document.createElement( "picture" );</script>
  <script class="picturefill" async="async" src="js/picturefill.min.js"></script>
  <link rel="stylesheet" href="css/main.css">
</head>

<body>
  <div class="container-grid container-2"></div>
  
  <div class="container-grid main">
  <?php
  session_start();
    $_SESSION['test']=$_POST['VID'];


include("cons/connect_Verses4Cards.php");
$conn=get_db_conn_verse();

$display_block ="";
$CardSz ="";
$Card_info['Size']="";
$Card_info['Narative']="";

//Function to build select options based on passed array
$CardSz = array($Card_info['Size'], $Card_info['Narative']); 
buildSelectOptions($CardSz);
function buildSelectOptions($options)
{
$optionsHTML = "<select name=\"CSize\">\r\n";

foreach($options as $id => $label)
{
$optionsHTML .= "<option value='{$id}'>{$label}</option>\n";
}

return $optionsHTML;
}   
 
//Run query to get the ID and Name from the table
//Then populate into an array
$clist_sql = "SELECT * FROM CSize ORDER BY GP";
$clist_res= mysqli_query($conn, $clist_sql) or die(mysqli_error($conn));
if (mysqli_num_rows($clist_res) < 1) {
	//this Card does not exist
	$display_block = "<p><em>You have selected an invalid Country.<br/>
	Please try again.</em></p>";
}
$CardSz = array();
while($Card_info = mysqli_fetch_array($clist_res)) {
       
        $CardSz[$Card_info['Size']] = $Card_info['Narative'];
}
$CardOptions = buildSelectOptions($CardSz);


?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="On this page the user choose the variables to align the verse on the paper" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>1066 Cards 4U - Variable Input Form</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="menu">
		<ul>




<div id="content">
<h1>Card Variable Input Form</h1>


<form name="Lists" action="Prnpdf.php" method="post">
   	
        <p><strong>Card Size</strong></p>
		<select name="CSize">
    <option value= "<?php echo $CardOptions;?>"  </option>
	</select>
<br/>
	
		<p><strong>Input Distance from top in mm:</strong> <input type="number" name="Top" value="50" min="20" max="200"/></p><br/>
		<p><strong>Input Font size in points:</strong> <select name="font">
		<option value="18" > 18 point</option>
		<option value="20" > 20 point</option>
		<option value="22" > 22 point</option>
		<option value="24" selected="selected" > 24 point</option>
		<option value="26" > 26 point</option>
		<option value="28" > 28 point</option>
		<option value="30"  > 30 point</option>
		<option value="32" > 32 point</option>
		</select></p><br/>
		
		<p><strong>Text Colour</strong></p>
		<select name="color">
		<option value="000000000" > Black</option>
		<option value="255215000" > Gold</option>
		<option value="192192192" > Silver</option>
		<option value="255000000" > Red</option>
		<option value="000255000" > Green</option>
		<option value="000000255" > Blue</option>
		<option value="255195203" > Pink</option>
		<option value="132112255" > Slate Blue</option>
		<option value="128000128" > Purple</option>
		<option value="168099030" > Brown</option>
		</select><br/>
		
		<p><strong>Text Font</strong></p>
		<select name="fontface">
		<option value="French Script MT" > French Script MT</option>
		<option value="Batavia" > Batavia </option>
		<option value="Algerian" > Algerian </option>
		<option value="Bladerunner" > Bladerunner </option>
		<option value="Brush Script" > Brush Script</option>
		<option value="Helterskelter" > Helterskelter</option>	
		<option value="Justice" > Justice</option>
		<option value="Magneto" > Magneto</option>
		<option value="Old English" > Old English</option>
		<option value="Sneakerhead Outline" > Sneakerhead Outline</option>
		<option value="Trendy" > Trendy</option>
		<option value="Vladimir Script" > Vladimir Script</option>
		</select><br/>
		
	
		
		<p><strong>Border Images </strong></p>
		<select name="image">
		<option value="images/empty.jpg" selected="selected"> No Border</option>
		<option value="images/Border10p.jpg" > Rose Border - Portrait</option>
		<option value="images/Border2p.jpg" > Chequered Border - Portrait</option>
		<option value="images/Border11p.jpg" > Ribbon Border - Portrait</option>
		<option value="images/Border5p.jpg" > Yellow Border - Portrait</option>
		<option value="images/Corner2p.jpg" > Leaf Corners - Portrait</option>
		<option value="images/corner1p.jpg" > Laurel Corners - Portrait</option>
		<option value="images/Border12P.jpg" > Holly Corner - Portrait</option>
		<option value="images/116070.jpg" > Ornate -Portrait</option>
		<option value="images/116122.jpg" > Ornate2 -Portrait</option>
		<option value="images/Border10l.jpg" > Rose Border -Landscape </option>
		<option value="images/Border2l.jpg" > Chequered Border -Landscape</option>
		<option value="images/Border11l.jpg" > Ribbon Border -Landscape</option>
		<option value="images/Border5l.jpg" > Yellow Border -Landscape</option>
		<option value="images/Corner2l.jpg" > Leaf Corners -Landscape</option>
		<option value="images/corner1l.jpg" > Holly Corner -Landscape</option>
		<option value="images/Border12L.jpg" > Yellow Border -Landscape</option>
		<option value="images/Holly1.jpg" > Holly-Landscape</option>
		<option value="images/Xmastree2.jpg" > Xmas Tree - Landscape</option>
		<option value="images/Decoborder1.png" > Art Deco Border - Portrait</option>
		</select><br /><br />
		

				
		<input type="submit" value="Submit"/>
		</form>

	</div><!-- end #content -->


</body>

</html>
</div>
<div class="container-grid footer">
  <?php include("inc/footer.php"); ?>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/outofview.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</div>
</body>
</html>
I hope someone can shine a light on why this code does not work correctly?
User avatar
Michalio
Moderator
Moderator
Posts: 339
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

that is the reason:

Code: Select all

  <div class="container-grid main">
  <?php
  session_start();
When you start the content (printing HTML) then the header is closed and the session_start() needs to have access to change the header. You need to start the session first, then you can print HTML or anything else
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
Post Reply