#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Grabbing info from database and displaying content


    I have been working on a project to establish if when a postcode entered is listed on my database. If the postcode is listed I want the information relayed back to the user confirming the postcode, level and if it is an area known for flooding (all the information is on the database).

    I have a basic form
    [code]
    <form method="post" action="database.php">
    <div align="center" class="style1">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <table width="80%" border="1" class="style1" bordercolor="#80A0BB" bcellpadding="0">
    <tr>
    <td><p align="center">&nbsp;</p>
    <p align="center">Enter postcode :
    <input name="postcodes" type="text" id="postcodes" size="15" />
    <?php echo $postcode ?> <br />
    <br />
    <br />
    <input name="Submit" type="submit" value="Click to Check" />
    </p>
    <p>&nbsp;</p></td>
    </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    </div>
    </form>

    this is then feed to the database.php page which connects to the database but doesnt return the correct value. Infact, any postcode I put in returns a value saying not listed on the database.

    My php code is
    [php]
    $postcode = "E9 7TR";
    $result = mysql_query("SELECT * FROM `TABLE 1` WHERE postcode='$postcode' ")
    or die(mysql_error());

    $row = mysql_fetch_array( $result );
    if(mysql_num_rows($row) >= 1)
    {
    echo "Postcode: ".$row['postcode'];
    echo "Level: ".$row['level'];
    echo "Floodlevel: ".$row['floodlevel'];
    } // display success message

    else { echo "The postcode entered is not listed on the database";
    }

    Any help would be appreciated!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,914
    Rep Power
    1045
    Hi,

    this is a PHP issue, which has absolutely nothing to do with MySQL.

    You apply mysql_num_rows() to an array (namely $row), which of course makes no sense. You need to pass $result to the function.

    Apart from this bug, there are several other issues:
    • Whatever book or online tutorial you're learning from, throw it away. This style of writing HTML and PHP is hopelessly outdated -- since 15 years or so. Using tables for layouting was fine in the 90s, but in the 21. century, people use CSS. The same goes for the PHP code. The old MySQL extension is dead since 10 years. People today use PDO or MySQLi.
    • You have several big security holes. Check the link in my signature to avoid typical mistakes.
    • Turn on your error reporting during development: error_reporting(-1)
    • Don't use SELECT *. It's inefficient, error-prone and potentially dangerous (because it will fetch any column, no matter how critical it is). Select specific columns. Don't use nonstandard (and nonsense) table names like TABLE 1. Name the table according to its content. Maybe postcodes?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers? There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo