The problem is 8-5 of PHP Programming with MySQL (2nd Edition) I tried this solu
ID: 3604788 • Letter: T
Question
The problem is 8-5 of PHP Programming with MySQL (2nd Edition)
I tried this solution but it didn't work as expected:
https://www.chegg.com/homework-help/PHP-Programming-with-MySQL-2nd-edition-chapter-8-problem-5RE-solution-9780538745840
I need it to look like the next 3 pictures:
Please help me fix this code:
First File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Enter a New Airline Survey</title>
<meta http-equiv="content-type" content="text/html; charset="iso-8859-1" />
</head>
<body>
<?php
$DBConnect = mysql_connect("localhost", "root", "root");
if($DBConnect === FALSE){
die("Unable to connect.");
}
$DBName = "airlinesurvey";
$SQLstring = "CREATE DATABASE $DBName";
if(mysql_query($SQLstring, $DBConnect)){
echo "<p>Database created successfully.</p>";
} else {
echo "<p>Database exists.</p>";
}
$DBConnect = mysql_connect("localhost", "root", "root", "airlinesurvey");
if($DBConnect === FALSE){
die("Could not connect.");
}
$TableName = "surveyreport";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if(@mysql_num_rows($QueryResult) > 0){
echo "$TableName table already exists";
} else {
$SQLstring = "CREATE TABLE $TableName (Survey_ID
SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
flightDate VARCHAR(20),flightTime VARCHAR(10),
flightNumber VARCHAR(10), startCity VARCHAR(50),
endCity VARCHAR(50), friendliness TINYINT,
luggageSpace TINYINT, seatComfort TINYINT,
cleanliness TINYINT, noiseLevel TINYINT)";
if(mysql_query($SQLstring, $DBConnect)){
echo "<p>Table created successfully.";
?>
<a href="EnterAirlineSurveyData.php">Go to the Survey Page</a>
<?php
}else {
echo mysql_error($DBConnect);
}
}
mysql_close($DBConnect);
?>
</body>
</html>
Second File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Enter a New Airline Survey</title>
<meta http-equiv="content-type" content="text/html; charset="iso-8859-1" />
</head>
<body>
<?php
$ShowForm = FALSE;
$Values = array('flightDate', 'flightTime', 'flightNumber',
'startCity', 'endCity', 'friendliness', 'luggageSpace',
'seatComfort', 'cleanliness', 'noiseLevel');
$Ratings = array('0', '1_star', '2_star', '3_star', '4_star', '5_star');
$Survey = array();
foreach($Values as $Value)
$Survey[$Value] = "";
if(isset($_POST['submit'])){
foreach($Values as $Value){
if((!isset($_POST[$Value])) || strlen(trim($_POST[$Value])) == 0 ){
echo "$Value is a required field. ";
$ShowForm = TRUE;
} else if(($Value == 'flightDate') && (preg_match('/^d{4}-d{1,2}$/', $_POST[$Value]) == 0)){
echo "'$Value' must be in the form yyyy-mm-dd! ";
$ShowForm = TRUE;
} else if(($Value == 'flightTime') && (preg_match('/^d{1,2}:d{2}*[ap]m$/i', $_POST[$Value]) == 0)){
echo "'$Value' must be in the form hh:mm AM/PM! ";
$ShowForm = TRUE;
} else {
$Survey[$Value] = stripslashes(trim($_POST[$Value]));
}
}
if($ShowForm === FALSE){
$DBConnect = mysql_connect("localhost", "root", "root", "airlinesurvey");
if($DBConnect === FALSE){
die("Unable to connect to the database");
} else {
$Date = stripslashes($_POST['flightDate']);
$Time = stripslashes($_POST['flightTime']);
$Number = stripslashes($_POST['flightNumber']);
$StartCity = stripslashes($_POST['startCity']);
$EndCity = stripslashes($_POST['endCityn']);
$Friend = stripslashes($_POST['friendliness']);
$Space = stripslashes($_POST['luggageSpace']);
$Comfort = stripslashes($_POST['seatComfort']);
$Clean = stripslashes($_POST['cleanliness']);
$Level = stripcslashes($_POST['noiseLevel']);
$SQLstring = "INSERT INTO surveyreport (flightDate, flightTime, flightNumber,
startCity, endCity, friendliness, luggageSpace, seatComfort, cleanliness,
noiseLevel) VALUES ($Date, $Time, $Number, $StartCity, $EndCity, Friend,
$Space, $Comfort, $Clean, $Level";
if(mysql_query($SQLstring, $DBConnect)){
echo "Records inserted successfully.";
} else {
echo "Records not inserted.";
}
}
mysql_close($DBConnect);
}
} else {
$ShowForm = TRUE;
}
if($ShowForm === TRUE){
?>
<form action="EnterAirlineSurveyData.php" method="POST">
<table>
<tbody>
<tr>
<td align="right">Flight Date</td>
<td align="left" colspan="5">
<input type="text" size="50" name="flightDate" value="<?php echo $Survey['flightDate']; ?>" />
<small>(must be in the form yyyy-mm-dd)</small>
</td>
</tr>
<tr>
<td align="right">Flight Time</td>
<td align="left" colspan="5">
<input type="text" size="50" name="flightTime" value="<?php echo $Survey['flightTime']; ?>" />
<small>(must be in the form hh:mm AM/PM)</small>
</td>
</tr>
<tr>
<td align="right">Flight Number</td>
<td align="left" colspan="5">
<input type="text" size="50" name="flightNumber" value="<?php echo $Survey['flightNumber']; ?>" />
</td>
</tr>
<tr>
<td align="right">Starting City</td>
<td align="left" colspan="5">
<input type="text" size="50" name="startCity" value="<?php echo $Survey['startCity']; ?>" />
</td>
</tr>
<tr>
<td align="right">Ending City</td>
<td align="left" colspan="5">
<input type="text" size="50" name="endCity" value="<?php echo $Survey['endCity']; ?>" />
</td>
</tr>
</tbody>
</table>
</form>
<?php
}
for($i=1;$i<5;++$i){
echo "".$Ratings[$i]." ";
}
echo " ";
?>
<p>Friendliness of customer staff</p>
<?php
for($i=1;$i<=5;++$i){
if($Survey['friendliness'] == $i){
echo "checked='checked'";
echo "/> ";
}
echo " ";
}
?>
<p>Space for luggage storage</p>
<?php
for($i=1;$i<=5;++$i){
if($Survey['friendliness'] == $i){
echo "checked='checked'";
echo "/> ";
}
echo " ";
}
?>
<p>Comfort of seating</p>
<?php
for($i=1;$i<=5;++$i){
if($Survey['friendliness'] == $i){
echo "checked='checked'";
echo "/> ";
}
echo " ";
}
?>
<p>Cleanliness of aircraft</p>
<?php
for($i=1;$i<=5;++$i){
if($Survey['friendliness'] == $i){
echo "checked='checked'";
echo "/> ";
}
echo " ";
}
?>
<p>Noise level of aircraft</p>
<?php
for($i=1;$i<=5;++$i){
if($Survey['friendliness'] == $i){
echo "checked='checked'";
echo "/> ";
}
echo " ";
}
?>
<input type="Submit" value="Clear Form" />
<input type="Submit" value="Submit" />
<a href="ViewAirlineSurveys.php">View All Surveys</a>
</body>
</html>
Third File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>View Airline Surveys</title>
<meta http-equiv="content-type" content="text/html; charset="iso-8859-1" />
</head>
<body>
<?php
$ShowForm = FALSE;
$Values = array('Survey_ID');
$Survey = array();
foreach($Values as $Value)
$Survey[$Value]="";
if(isset($_POST['submit'])){
foreach($Values as $Value){
if((!isset($_POST[$Value])) || (strlen(trim($_POST[$Value]))) == 0 ){
echo "'$Value' is a required value. ";
$ShowForm = TRUE;
} else {
$Survey[$Value]=stripslashes(trim($_POST[$Value]));
}
}
if($ShowForm === FALSE){
if(empty($_POST['Survey_ID']))
echo "Enter all the values. Click your browser's Back button to return to the form.";
} else {
$DBConnect = mysql_connect("localhost", "root", "root", "airlinesurvey");
if($DBConnect === FALSE){
die("Unable to connect to the database.");
} else {
$ID = stripslashes($_POST['Survey_ID']);
$SQLstring = "SELECT * FROM surveyreport WHERE Survey_ID = '$ID'";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if(@mysql_num_rows($QueryResult) > 0){
while(($Row = mysql_fetch_assoc($QueryResult))){
echo "Flight Date: ".$Row['flightDate']."";
echo "Flight Time: ".$Row['flightTime']."";
echo "Flight Number: ".$Row['flightNumber']."";
echo "Start City: ".$Row['startCity']."";
echo "End City: ".$Row['endCity']."";
echo "Friendliness: ".$Row['friendliness']."_star";
echo "Luggage Space: ".$Row['luggageSpace']."_star";
echo "Seat Comfort: ".$Row['seatComfort']."_star";
echo "Cleanliness: ".$Row['cleanliness']."_star";
echo "Noise Level: ".$Row['noiseLevel']."_star";
}
} else {
echo "No results found.";
}
}
mysql_close($DBConnect);
}
} else {
$ShowForm = TRUE;
}
?>
</body>
</html>
I tried using the solution provided by Chegg but it wasn't working for me.
Enter a New Airline Survey Airline Suivev Entrv Notice: Undefined offset 5 in C:lwamp wwwEnterAirlineSuveyData php on line 104 Flight Date 2010-12-23 Flight Time 10:30 AMmust be in the form hhzmm AMPM) (must be in the form yyy-mm-dd) Flight Number 325 Airline SunCoast Starting City Denver Ending City Chicago Poor Fair Good Excellent Friendliness of customer staf C Space for luggage storage C Comfort of seating Cleanliness of aircraft C Noise level of aircraft Clear Form Submit View All SurveysExplanation / Answer
>Make sure you have created table and database and connection is successfull, ( you can use phpmyadmin on wamp server to check it in gui mode)
> call database connect script on clicking submit button.
I find that the code is fine, it happens often that implementation errors are there in mysql,
may be you should check your mysql for compatibility,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.