0

I am having a reall issue with an assignment I am on. Here is the Assignemnt and attached are what I have for the .html and .php files. My .html is displaying as code on a web page vice the desired formatting. Any advice?

Write a PHP script that obtains a URL and its description from a user and stores the information into a database using MySQL. Create and run a SQL script with a database named URL and a table named Urltable. The first field of the table should countain 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 second 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 a table.

Also here are some added instructions from my proffesor.

The scripts of database for this chapter are attached here.

Please create a user account as the video in course materials to connect mySQL. We should use:

username: iw3htp

password: passowrd (yes its misspelled, I did it on accident when I established the account)

Attachments
<!DOCTYPE html>

<html>
   	<head>
      		<meta charset = "utf-8">
      		<title>Week 7 Chapter 19.9</title>
      		<style type = "text/css">
         		body  { font-family: comic sans ms;
				background-color: yellow;
				color: black }
			h1    { font-weight: bold;
				text-decoration: underline; }
			table { background-color: purple;
				color: yellow;
				border-collapse: collapse;
				border: 1px solid gray; }
			td    { padding: 5px; }
			tr:nth-child(odd) {
				background-color: green; 
				color: orange; }
      		</style>
   	</head>
   
   	<body>

			$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", "passowrd" ) ) )
					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 required fields and select Register.</p>" );

			if ( $iserror )
			{
				print( "<p class = 'error'>Fields with "*" are required fields.</p>" );
			}

			print( "<!-- post form data to table.php -->
				<form method = 'post' action = 'table.php'>
				<h1> Required Information</h1>" );

			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>
4
Contributors
10
Replies
29
Views
2 Years
Discussion Span
Last Post by matrixdevuk
0

Sorry here is the .php, the site will not let me add it as a ,php so I just converted it to an html.

Attachments
<!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 gray; }
    			tr:nth-child(even) { background-color: white; }
    			tr: first-child { background-color: lightgreen; }
    		</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>
0

I believe ++$counter is supposed to be $counter++. Also, in your OP you mentioned your password is 'passowrd'. In your code, it's 'password'.
Change this:

if( !( $database = mysql_connect( "localhost",
                "iw3htp", "password" ) ) )

...to this:

if( !( $database = mysql_connect( "localhost",
                "iw3htp", "passowrd" ) ) )

Also, change your loop to this:

for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
{
    print( "<tr>" );
    foreach ( $row as $key => $value )
    {
        print( "<td>$value</td>" );
    }
    print( "</tr>" );
}

Or maybe even this...

while ($row = mysql_fetch_assoc($result))
{
    print("<tr>");
    foreach ($row as $key => $value)
    {
        print("<td>$value</td>");
    }
    print("</tr>");
}

note: none of the above code has been tested

0

you can make your code more simple by trying not to mix your php with html too much. Here is a snippet for you results page. Using a simple foreach loop to get your results and echo them into a table cell.

<?php

    $connect = mysql_connect('localhost', 'iw3htp', 'password');
        if(!$connect){
            die('Connection Error: ' . mysql_error());
        }

    $db_select = mysql_select_db('URLS', $connect);
        if(!$db_select){
            die('Connection Error: ' . mysql_error());
        }


    $sql = "SELECT * FROM URLS";

    $query = mysql_query($sql);

    $results = mysql_fetch_assoc($query);
?>

    <table>
        <tbody>
            <?php foreach($results as $result): ?>
            <tr>
                <td><?php echo $result['url']; ?></td>
                <td><?php echo $result['description']; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

Edited by gabrielcastillo

0

Matrixdevuk -> That is great thank you

Attached is what the html document looks like when opened in a web page. Any ideas why?

Attachments Why_does_it_look_like_this.jpg 262.46 KB
0

as per the title of your thread where it says "XAMPP", you need to start the xampp control panel and then start the apache and mysql services.

then direct your browser to localhost/youphp_file.php

You cannot save pages with PHP codes in them as .hml document. You can however save php docs with .inc, php3, and if supported phtml or as defined in your directoryIndex directives.

0

gabrielcastillo-> Where are you suggesting I place this code? in my html or my php? Please forgive my ignorance, I really am bad at all this, just sort of blundering through at this point. Thanks

0

you place the code in your php file, which ever one, you are going to display the database results.

For instance, if you have two pages. Page 1 = register.php and Page 2 records.php

the records.php will hold the code to display the database records. The register.php will hold the code to display the form inputs, to add new records to the database.

0

Wow, thank you so much for all the help. This was my first time using DaniWeb, and it was definitely a good one! I feel really dumb for forgetting to log into the xampp control first, but the code issues were a great help. Thank you again!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.