Question
Trying to store information using $_COOKIE autoglobal. PLEASE READ MY EXPLANATION BEFORE RESPONDING. Here's how the pages should work: AvailableOpportunities.php displays a list of open
Trying to store information using $_COOKIE autoglobal.
PLEASE READ MY EXPLANATION BEFORE RESPONDING.
Here's how the pages should work: AvailableOpportunities.php displays a list of open internships options, each with it's own "Available" link. Clicking a link will take you to RequestOpportunity.php that displays "Your request for opportunity # $OpportunityID has been entered on $DisplayDate." So the $OpportunityID is either 1, 2, 3, 4, or 5 depending on which link was clicked ($DisplayDate is the date that the link was clicked). Below that is a link back to the AvailabilitiesOpportunities.php page. Upon returning to the original page, the link should now say "Selected" and there should be a message over the top of the internships table that says "You last requested an internship opportunity on $LastRequestDate." $LastRequestDate is what is being stored/sent by the cookie.
The problem is that the cookie is not working so the AvailableOpportunities.php is not being updated. I've included the code for both pages below.
AvailableOpportunities.php:
College Internship
Available Opportunities
//complete Exercise #3 (for AvailableOpportunities.php) on Page 509 here --------------------- if(isset($_REQUEST['internID'])) $InternID = $_REQUEST['internID']; else $InternID = -1;
//exercise #2 from page 527 if(isset($_COOKIE['LastRequestDate'])) $LastRequestDate = $_COOKIE['LastRequestDate']; else $LastRequestDate = "";
$errors = 0;
include("../config.php"); $DBConnect = new mysqli ($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']);
if ($DBConnect === FALSE) { echo "
Unable to connect to the database server. " . "Error code " . mysqli_errno() . ": " . mysqli_error() . "
"; ++$errors; } else { $DBName = "internships"; $result = mysqli_select_db($DBConnect, $DBName); if ($result === FALSE) { echo "Unable to select the database. " . "Error code " . mysqli_errno($DBConnect) . ": " . mysqli_error($DBConnect) . "
"; ++$errors; } } $TableName = "interns"; if ($errors == 0) { // complete Exercise #5 Page 510 here. $SQLstring = "SELECT * FROM $TableName WHERE internID='$InternID'"; $QueryResult = mysqli_query($DBConnect, $SQLstring); if ($QueryResult === FALSE) { echo "Unable to execute the query. " . "Error code " . mysqli_errno($DBConnect) . ": " . mysqli_error($DBConnect) . "
"; ++$errors; } else { if (mysqli_num_rows($QueryResult) == 0) { echo "Invalid Intern ID!
"; ++$errors; } } } if ($errors == 0) { $Row = mysqli_fetch_assoc($QueryResult); $InternName = $Row['first'] . " " . $Row['last']; } else $InternName = ""; $TableName = "assigned_opportunities"; $ApprovedOpportunities = 0;//complete Exercise #7 on page 511 here------------------------------- $SQLstring = "SELECT COUNT(opportunityID) FROM $TableName "."WHERE internID='$InternID' ". "AND date_approved IS NOT NULL"; $QueryResult = mysqli_query($DBConnect, $SQLstring); if (mysqli_num_rows($QueryResult) > 0) { $Row = mysqli_fetch_row($QueryResult); $ApprovedOpportunities = $Row[0]; mysqli_free_result($QueryResult); } $SelectedOpportunities = array(); $SQLstring = "SELECT opportunityID FROM $TableName " . " WHERE internID='$InternID'"; $QueryResult = mysqli_query($DBConnect, $SQLstring); if(mysqli_num_rows($QueryResult) > 0) { $Row = mysqli_fetch_row($QueryResult); do { $SelectedOpportunities[] = $Row; $Row = mysqli_fetch_row($QueryResult); } while($Row); mysqli_free_result($QueryResult); }
/*if (mysqli_num_rows($QueryResult) > 0) { while (($Row = mysqli_fetch_row($QueryResult)) !== FALSE) $SelectedOpportunities[] = $Row[0]; mysqli_free_result($QueryResult); }*/
$AssignedOpportunities = array(); $SQLstring = "SELECT opportunityID FROM $TableName " . " WHERE date_approved IS NOT NULL"; $QueryResult = mysqli_query($DBConnect, $SQLstring); if (mysqli_num_rows($QueryResult) > 0) { while (($Row = mysqli_fetch_row($QueryResult)) !== FALSE) $AssignedOpportunities[] = $Row[0]; mysqli_free_result($QueryResult); } $TableName = "opportunities"; $Opportunities = array(); //complete Exercise #10 on Page-512 ------------------- $SQLstring = "SELECT opportunityID, company, city, start_date, end_date, position, description "."FROM $TableName"; $QueryResult = mysqli_query($DBConnect, $SQLstring); if(mysqli_num_rows($QueryResult) > 0) { $Row = mysqli_fetch_assoc($QueryResult); do { $Opportunities[] = $Row; $Row = mysqli_fetch_assoc($QueryResult); } while($Row); //mysqli_free_result($QueryResult); }
mysqli_close($DBConnect); /*if (mysqli_num_rows($QueryResult) > 0) { while (($Row = mysqli_fetch_assoc($QueryResult)) !== FALSE) $Opportunities[] = $Row; mysqli_free_result($QueryResult); } mysqli_close($DBConnect);*/
//step 3 from page 528 if(!empty($LastRequestDate)) echo "
You last requested an internship opportunity on $LastRequestDate.
";$Row = mysqli_fetch_assoc($QueryResult); do { echo "
echo "
Company | "; echo "City | "; echo "Start Date | "; echo "End Date | "; echo "Position | "; echo "Description | "; echo "Status | "; echo "
---|---|---|---|---|---|---|
" . htmlentities($Opportunity['company']) . " | "; echo "" . htmlentities($Opportunity['city']) . " | "; echo "" . htmlentities($Opportunity['start_date']) . " | "; echo "" . htmlentities($Opportunity['end_date']) . " | "; echo "" . htmlentities($Opportunity['position']) . " | "; echo "" . htmlentities($Opportunity['description']) . " | "; echo ""; if (in_array($Opportunity['opportunityID'], $SelectedOpportunities)) echo "Selected"; else { if ($ApprovedOpportunities>0) echo "Open"; else echo "Available"; } echo " | "; echo "
Log Out
";?>
RequestOpportunities.php:
//complete exercise #4 on page 522-523 here-----------------------
if(isset($_GET['internID'])) $InternID = $_GET['internID']; else { $Body .= "
You have not logged in or registered. Please return to the Registration/Log In page
"; ++$errors; }if($errors == 0) { if(isset($_GET['opportunityID'])) $OpportunityID = $_GET['opportunityID']; else { $Body .= "
You have not selected an opportunity. Please return to the ". "Available Opportunities page
"; ++$errors; } }if ($errors == 0) { include("../config.php"); $DBConnect = new mysqli ($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']); if ($DBConnect === FALSE) { $Body .= "
Unable to connect to the database " . " server. Error code " . mysqli_errno() . ": " . mysqli_error() . "
"; ++$errors; } else { $DBName = "internships"; $result = mysqli_select_db($DBConnect, $DBName); if ($result === FALSE) { $Body .= "Unable to select the database. " . "Error code " . mysqli_errno($DBConnect) . ": " . mysqli_error($DBConnect) . "
"; ++$errors; } } } //complete exercise #6,7,8 on page 523-524 here$DisplayDate = date("l, F j, Y, g: i A"); $DatabaseDate = date("Y-m-d H:i:s"); if($errors == 0) { $TableName = "assigned_opportunities"; $SQLstring = "INSERT INTO $TableName (opportunityID, internID, date_selected) VALUES ('$OpportunityID', '$InternID', '$DatabaseDate')"; $QueryResult = mysqli_query($DBConnect, $SQLstring); if(!$QueryResult) { $Body .= "
Unable to execute the query. Error code ".mysqli_errno($DBConnect).": ".mysqli_error($DBConnect)."
"; ++$errors; } else { $Body .= "Your request for opportunity # $OpportunityID has been entered on $DisplayDate.
"; } mysqli_close($DBConnect); }if($InternID > 0) $Body .= "
Return to the Available Opportunities page.
"; else $Body .= "Please Register or Log In to use this page.
";if($errors == 0) @setcookie("LastRequestDate", urlencode($DisplayDate), time()+60*60*24*7, "/"); /*
The @ is not causing the issue. I have tested the program without it. It's here because my instructor wants us to include a file so he can just change the variables once when looking at our code. Unfortunately, the include statement is read as a header by the server and causes an error. The @ is the only compromise given is requirments. */ ?>
College Internship
Opportunity Requested
echo $Body; ?>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started