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)

Recommended Answers

All 10 Replies

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

What is your issue? Where is the code failing?

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

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>

Matrixdevuk -> That is great thank you

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

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.

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

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.

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!

Glad we could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.