Php – Retrieve data from MySql database using html form and php with demo

php

I am new at php and I have read almost all forums, but no result!!! I am trying to retrieve some data from my mySql db using an html form and a php file. The html form called choose.htm is as follows:

    <form name="choose" method = "POST" action = search.php>
    <table>
    <tr>
    <tr><td height="3"></td></tr>
    <td width="60"><font1>Denomination</font1></td>
    <td><Select name = denom>
        <option value="" selected>All</option>
              <option value="half">Half Cents</option>
        <option value="large">Large Cents</option>
        <option value="bust">Bust Dollars</option>
        <option value="morgan">Morgan Dollars</option>
    </Select></td></tr>
    <tr>
    <tr><td height="3"></td></tr>
    <td width="60"><font1>Year</font1></td>
    <td><Select name = year>
        <option value="" selected>All</option>
        <option value="1793"><font4>1793</font></option>      
        <option value="1794">1794</option>    
        <option value="1795">1795</option>    
        <option value="1796">1796</option>    
    </Select></td></tr>

    <tr>
    <tr><td height="3"></td></tr>
    <td width="60"><font1>Picture</font1></td>
    <td><Select name = picture>
        <option value="" selected>All</option>
        <Option value="liberty">Liberty Cap</Option>
        <Option value="draped">Draped Bust</Option>
        <Option value="classic">Classic Head</Option>
        <Option value="chain">Chain Reverse</Option>
    </Select></td></tr>

   <tr><td height="3" colspan="2"></td></tr>
   <tr><td><font1>Text</b></font1></td>
   <td><input name=text type=text></td></tr>
   </Select></td></tr>

   <tr><td><input name=look type=submit value=Submit></td></tr>
   </form>

And the php file called search.php is as follows:

    <html>
    <body>

    <?php
    $username="root";
    $password="";
    $database="xxxxxxxx";

    mysql_connect(localhost,$xxxxxx,$xxxxxx);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM coins";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    echo "<b><center>US Coins</center></b><br><br>";

    $i=0;
    while ($i < $num) {

    $denom=$_POST["denom"];
    $year=$_POST["year"];
    $picture=$_POST["picture"];
    $text=$_POST["text"];

    echo "$denom<br>Year: $year<br>Picture: $picture<br>Text: $text<br><br>";

    $i++;
    }
    ?>

    </body>
    </html>

I am getting an output at the number of rows in my db, with the options that I have selected:

half
Year: 1793
Picture:
Text:

half
Year: 1793
Picture:
Text:

…….. goes on.

I couldn't solve the problem. All helps will be very much appreciated…

Sarp

Best Solution

You will need some better code if you want to find your error.

1) http://validator.w3.org - put your HTML code in that form and fix ALL the errors first.
2) Parse all the PHP code before you start the HTML. Else the user will see just half the loaded page for a sec. And thats not quite clean.
3) @mysql_select_db($database) or die() is wrong. Handle the PHP errors.
4) Use mysql_fetch_assoc instead of mysql_numrows.

You can thank me for cleaning up your messy code later.

<?php
    $host = 'localhost';
    $username="root";
    $password="";
    $database="xxxxxxxx";


    $connection = @mysql_connect($host,$username,$password);
    $selection = @mysql_select_db($database, $connection);

    if(!$connection || !$selection){
        echo 'Connection failed. Contact webmaster and ask if his connection settings are okay.';
    }

    $query = "SELECT * FROM coins";
    $mysqlquery = mysql_query($query);

    if($mysqlquery){ // Query succeed! :D
        $result = mysql_fetch_assoc($mysqlquery); // We'll parse em later
    }
    else
    {
        echo 'MySQL Query failed! Give the webmaster a slap for his bad coding.';
    }

    mysql_close();

    // We've handeled all the PHP stuff, now we can get started with printing everything
?>

<html>
<body>

    <?php
        echo '<b><center>US Coins</center></b><br><br>'; // This HTML code needs cleanup too

        $i = 0;
        while($i < $num){
            echo htmlspecialchars($_POST['denom']).'<br>Year: '. htmlspecialchars($_POST['year']) .'<br>Picture: '. htmlspecialchars($_POST['picture']) .'<br>Text: '. $_POST['text'] .'<br><br>';
            $i++;
        }
    ?>

</body>
</html>