19.9 Write a PHP script that obtains a URL and its description from a user and s
ID: 3692308 • Letter: 1
Question
19.9 Write a PHP script that obtains a URL and its description from a user and stores the information into a databaseusing MySQL. create and run a SQL script with a database named URLand a table named Urltable. the first field of the table should contain an actual URL, and the second, which is named description, should contain a description of the URL. Use www.deitel.com as the first URL, and input COOL Site! as its description. The ssecon URL should be www.php.net, and the description should be The Official PHP site. After each new URL is submitted, print the contents of the database in the table.
Please include the following three files for your solution (.html, .php, and .sql).
Explanation / Answer
Any queries please comment
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Search Results</title>
<style type = "text/css">
body { font-family: sans-serif;
background-color: lightyellow; }
table { background-color: lightblue;
border-collapse: collapse;
border: 1px solid gray; }
td { padding: 5px; }
tr:nth-child(odd) {
background-color: white; }
</style>
</head>
<body>
<?php
$URL = isset($_POST["URL" ]) ? $_POST["URL" ] : "";
$Description = isset($_POST["Description" ]) ? $_POST["Description" ] : "";
$iserror = false;
$formerrors =
array( "URLerror" => false, "Descriptionerror" => false);
$inputlist = array( "URL" => "URL", "Description" => "Description" );
if ( isset( $_POST["submit"] ) )
{
if ( $URL == "" )
{
$formerrors[ "URLerror" ] = true;
$iserror = true;
}
if ( $Description == "" )
{
$formerrors[ "Descriptionerror" ] = true;
$iserror = true;
}
$query = "INSERT INTO urltable ".
"( URL, Description )".
"VALUES ( '$URL', '$Description')";
if( !( $database = mysql_connect( "localhost",
"iw3htp", "password" ) ) )
die( "<p>Could not connect to database</P>");
if ( !mysql_select_db( "URLs", $database ) )
die( "<p>Could not open URLs database</p>" );
if ( !( $result = mysql_query( $query, $database ) ) )
{
print( "<p>Could not execute query!</P>" );
die( mysql_error() );
}
mysql_close( $database );
die();
}
print( "<h1>URL Description Form</h1>
<p>Please fill in all fields and click Register.</p>" );
if ( $iserror )
{
print( "<p class = 'error'>Fields with * need to be filled in properly.</p>" );
}
print( "<!-- post form data to table.php -->
<form method = 'post' action = 'table.php'>
<h2> Required Information</h2>" );
foreach ( $inputlist as $inputname => $inputalt)
{
print( "<div><label>$inputalt:</label><input type = 'text'
name = '$inputname' value = '". $$inputname. "'>" );
if ( $formerrors[ ( $inputname ). "error" ] == true )
print( "<span class = 'error'>*</span>" );
print( "</div>" );
}
print("<p class = 'head'><input type = 'submit' name = 'submit'
value = 'Register'></p></form></body></html> ");
?>
</body>
</html>
Table.h
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Search Results</title>
<style type = "text/css">
table { background-color: lightblue;
border-collapse: collapse;
border: 1px solid gray; }
th, td { padding: 5px; border: 1px solid yellow; }
tr:nth-child(even) { background-color: red; }
tr: first-child { background-color:white; }
</style>
</head>
<body>
<?php
$query = "SELECT * FROM urltable";
if( !( $database = mysql_connect( "localhost",
"iw3htp", "password" ) ) )
die( "<p>Could not connect to database</P>");
if ( !mysql_select_db( "URLs", $database ) )
die( "<p>Could not open URLs database</p></body></html>" );
if ( !( $result = mysql_query( $query, $database ) ) )
{
print( "<p>Could not execute query!</P>" );
die( mysql_error() );
}
?>
<h1>URL Descriptions</h1>
<table>
<caption>Descriptions stored in the database</caption>
<tr>
<th>URL</th>
<th>Description</th>
</tr>
<?php
for ( $counter = 0; $row = mysql_fetch_row( $result ); ++$counter )
{
foreach ( $row as $key => $value )
print( "<td>$value</td>" );
print( "</tr>" );
}
mysql_close( $database );
?>
</table>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.