1

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

6
  • the html attribute name needs quotes ex name="foo" not name = foo Commented Mar 19, 2012 at 17:03
  • @PatrickLorio — Not true. Quotes are optional in that situation. They are good style, but not required. Commented Mar 19, 2012 at 17:05
  • 1
    Just like not capitalizing your tags or putting spaces between the properties and values. Commented Mar 19, 2012 at 17:09
  • @PatrickLorio Not Required, but ugly as f*** Commented Mar 19, 2012 at 17:30
  • Please do not judge me folks. This is what I learned from tutorials on the net... Commented Mar 22, 2012 at 22:18

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks vary much Thew, why wait for later to thank! I am learning a lot but the problem is that the query you typed returns empty..
0

$_POST contains the values submitted with the form. There is no reason to be printing it in a loop, as in your case each key has a single value. If you want to output data from your MySQL results you will need to use something like mysql_fetch_array in a while loop, then output the values of each of the arrays this returns.

A good place to start is the PHP MySQL Manual which contains a working example from which you can easily deduce the basics (even if you don't feel like R'ing TFM).

2 Comments

Dear bkconrad,Indeed thanks very for the manual you have mentioned. I learned a lot from it. Still, I am confused about how I will use the html form and combine the values in it with the php result file.
Dear bkconrad,Indeed thanks very for the manual you have mentioned. I learned a lot from it. Still, I am confused about how I will use the html form and combine the values in it with the php result file. – Sarp
0

If you do not have any data in your database, the query will return an empty result. So, make sure that you have some data in your database.

try something like this...

$num=mysql_fetch_array(mysql_query("select * from coins"))
while ($i<$num) {
echo $num . "<br />";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.