Question

I am having an issue. When i press submit to test the code, it goes to...

I am having an issue. When i press submit to test the code, it goes to a blank screen. Please, will some one assist me? Thanks!

<!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>Sign Guest Book</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />

</head>
<body>

<?php

if (empty($_POST['?rst_name']) || empty($_POST['last_name']))    
echo "<p>You must enter your ?rst and last name! Click your browser's Back button to return to the Guest Book form.</p>";
else {    
$DBConnect = @mysql_connect("localhost", "root", "");    
if ($DBConnect === FALSE)          
echo "<p>Unable to connect to the database server.</p>"              
. "<p>Error code " . mysql_errno()              
. ": " . mysql_error() . "</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 ?rst 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_INCREMENT PRIMARY KEY,       
    last_name VARCHAR(40), first_name VARCHAR(40))";       
    $QueryResult = @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['?rst_name']);              
     $SQLstring = "INSERT INTO $TableName               
     VALUES(NULL, '$LastName','$FirstName')";              
     $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 "<h1>Thank you for signing our guest book!</h1>";         
       }         
       mysql_close($DBConnect);    
      }
     }

?>
</body>
</html>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here for this demonstration using WAMP(Windows, Apache , MySQl , PHP) server. A web page with name "gestbook.html" is created and modified php code is kept in the file with name "connect.php" . Following section gives details about above files.

guestbook.html : (Used for demo purpose only)


<html>
<head>
<!-- title for web page -->
<title>Guest Book</title>
</head>
<body>
<!-- form with action and method attribute -->
<form action="connect.php" method="POST">
<!-- first Name -->
First Name :
<input type="text" name="first_name" placeholder="Enter First Name"/>
<br/>
<br/>
<!-- last Name -->
Last Name :
<input type="text" name="last_name" placeholder="Enter Last Name"/>
<br>
<br/>
<input type="submit"/>
</form>
</body>
</html>
***********************************

connect.php :


<!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>Sign Guest Book</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
//if first_name and last_name is empty then display message
if (empty($_POST['first_name']) || empty($_POST['last_name']))
echo "<p>You must enter your ?rst and last name! Click your browser's Back button to return to the Guest Book form.</p>";
else {
//@mysql_connect() passing host=localhost , username=root and password=""
$DBConnect = @mysql_connect("localhost", "root", "");
if ($DBConnect === FALSE)
echo "<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysql_errno()
. ": " . mysql_error() . "</p>";
else {
//database name
$DBName = "guestbook";
if (!@mysql_select_db($DBName, $DBConnect)) {
//creating database
$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);
//table name
$TableName = "visitors";
$SQLstring = "CREATE TABLE if not exists $TableName(countID SMALLINT
NOT NULL AUTO_INCREMENT PRIMARY KEY,
last_name VARCHAR(40), first_name VARCHAR(40))";
$QueryResult = @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>";
//taking value from the form
$LastName = stripslashes($_POST['last_name']);
$FirstName = stripslashes($_POST['first_name']);
//sql query for inserting record
$SQLstring = "INSERT INTO $TableName
VALUES(NULL, '$LastName','$FirstName')";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult === FALSE)
//if failure then display below message
echo "<p>Unable to execute the query.</p>"
. "<p>Error code " . mysql_errno($DBConnect)
. ": " . mysql_error($DBConnect) . "</p>";
else
//if success then display this message
echo "<h1>Thank you for signing our guest book!</h1>";
// }
//closing connection
mysql_close($DBConnect);
}
}
?>
</body>
</html>

==========================================================================

Output : Deploy/put the application in the www folder of wamp server and browse for the guestbook.html. will get the screens as shown below

Screen 1 :guestbook.html

Screen 2 : Enter value for firstname and lastname , as shown in below screen

Screen 3 : After clicking on submit , will get the screen as shown below for first record only

Screen 3 : After this navigate to phpmyadmin and login with username root and keep password blank and will see the database will be created with name guestbook and table with name visitors. as shown in below screen

Screen 4 : For second record suppose enter firstname = sachin and lastname=tendulkar will get the screen as shown below.

Screen 5 : Database table in phpmyAdmin

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
I am having an issue. When i press submit to test the code, it goes to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PHP Programming In this project, you will create a Web page that allows visitors to your...

    PHP Programming In this project, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a database. Create a new document in your text editor and type the <!DOCTYPE> declaration, <html> element, document head, and <body> element. Use the strict DTD and “Guest Book” as the content of the <title> element. Add the following text and elements to the document body: <h2>Enter your name to sign our guest book</h2>...

  • Hello, I was wondering if someone could help me with this PHP and HTML problem. Basically,...

    Hello, I was wondering if someone could help me with this PHP and HTML problem. Basically, there is code for a multiplication table and i need to change it to support the following. Make the page background (not table background) to change among three colors of your choice. Use the drop-down list to specify the size of the multiplication table to be 8, 10, 12, and 14 Put the multipliers in the first column and the multiplicand on the first...

  • For this code below, I need to add the form information to mysql when I click...

    For this code below, I need to add the form information to mysql when I click on submit, at the same time when I click on submit I need to move to another page like Welcome.php WITH NOTE THAT THE ENTERED INFORMATION NOW ALREADY IN DATABASE how can I male that with my code below? THANKS ................................................................................................................................................   <form method="POST"> <div class="container">    <label for="fname"><b>First Name</b></label> <input type="text" placeholder="Enter First Name" name="fname" required> <label for="lname"><b>Last Name</b></label> <input type="text" placeholder="Enter Last Name"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT