Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you please explane this assignment? It gave me error? <!DOCTYPE html> <html>

ID: 3704622 • Letter: C

Question

Can you please explane this assignment? It gave me error?

<!DOCTYPE html>
<html>
<head>
<title>Sign Guest Book</title>
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST['last_name']))
echo "<p>You must enter your first and last name! Click your browser's Back button to return to the Guest Book form.</p>";
  
else {
$DBConnect = @mysql_connect("host", "user", "password");
if ($DBConnect === FALSE)
echo "<p>Unable to connect to the database servr.</p>"
. "<p>Error code " . mysql_errno()
. ":" . mysql_errno(). "</p>";
  

else {
$DBName = "guestbook";
if (!@mysql_select_db($DBName, $DBConnect)){
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
echo "<p>Unable to execute the query.</P>"
."<p>Error code" . mysql_errno($DBConnect)
. ":" .mysql_error($DBConnect). "</P>";
else
echo "<p>You are the first visitor!</p>";
  
}
mysql_select_db($DBName, $DBConnect);

$TableName = "visitors";
$SQLstring = "SHOW TABLES LIKE '$TableName'";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) == 0) {
$SQLstring = "CREATE TABLE $TableName
(countID SMALLINT
NOT NULL AUTO_INCEMENT PRIMARY KEY,
last_name VARCHAR(40), first_name VARCHAR(40))";
$QueryReult = @mysql_query($SQLstring,
$DBConnect);
if ($QueryResult===FALSE)
echo "<p>Unable to create the table.</p>"
. "<p>Error code " . mysql_errno($DBConnect)
. ":" . mysql_error($DBConnect).
"</p>";
  
$LastName = stripslashes($_POST['last_name']);
$FirstName = stripslashes($_POST['first_name']);
$SQLstring = "INSERT INTO $TableName VALUES(NULL, 'LastName','$FirstName')";
$SQLstring = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
echo "<p>Unable to execute the query.</p>"
. "<p>Error code" . mysql_errno($DBConnect)
. ":" . mysql_error($DBConnect). "</P>";
else
echo "<h1>Thank you signing our guest book!</h1>";
}
mysql_close($DBConnect)

}
}
?>
</body>
</html>

Explanation / Answer

The error is solved see the below code

<!DOCTYPE html>
<html>
<head>
<title>Sign Guest Book</title>
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST['last_name']))
echo "<p>You must enter your first and last name! Click your browser's Back button to return to the Guest Book form.</p>";
  
else {
$DBConnect = @mysql_connect("host", "user", "password");
if ($DBConnect === FALSE)
echo "<p>Unable to connect to the database servr.</p>"
. "<p>Error code " . mysql_errno()
. ":" . mysql_errno(). "</p>";
  
else {
$DBName = "guestbook";
if (!@mysql_select_db($DBName, $DBConnect)){
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
echo "<p>Unable to execute the query.</P>"
."<p>Error code" . mysql_errno($DBConnect)
. ":" .mysql_error($DBConnect). "</P>";
else
echo "<p>You are the first visitor!</p>";
  
}
mysql_select_db($DBName, $DBConnect);
$TableName = "visitors";
$SQLstring = "SHOW TABLES LIKE '$TableName'";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) == 0) {
$SQLstring = "CREATE TABLE $TableName
(countID SMALLINT
NOT NULL AUTO_INCEMENT PRIMARY KEY,
last_name VARCHAR(40), first_name VARCHAR(40))";
$QueryReult = @mysql_query($SQLstring,
$DBConnect);
if ($QueryResult===FALSE)
echo "<p>Unable to create the table.</p>"
. "<p>Error code " . mysql_errno($DBConnect)
. ":" . mysql_error($DBConnect).
"</p>";
  
$LastName = stripslashes($_POST['last_name']);
$FirstName = stripslashes($_POST['first_name']);
$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$LastName','$FirstName')";
$SQLstring = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
echo "<p>Unable to execute the query.</p>"
. "<p>Error code" . mysql_errno($DBConnect)
. ":" . mysql_error($DBConnect). "</P>";
else
echo "<h1>Thank you signing our guest book!</h1>";
}
mysql_close($DBConnect);
}
}
?>
</body>
</html>

Explanation

Suppose firstname or lastname is empty display custom message otherwise establish connection with database named guestbook and if table is not created, create table visitors otherwise store firstname and lastname into visitors table