Results 1 to 4 of 4

Thread: Extracting Data From MySQL using php

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extracting Data From MySQL using php

    Im knew to the world of programming and have taken a liking to it very well. I am currently doing a project that requires an HTML form for a user to input a Last Name, First Name, and State and then click the submit button. After submitting information I need the script to query MySQL Database to only show what the user has submitted and not every entry into the database table; then the result should be shown on a HTML page in a table format. Here is what I have for the HTML Form:

    Code:
    <form action="SelectDirectory.php" method="get">
        	<fieldset>
          <br />
          Last Name: <input name="LastName" size="15" /><br />
          <br />
          First Name: <input name="FirstName" size="15" /><br />
          <br />
          </fieldset>
          <br />
          <fieldset>
          <br />
          State: <select name="State">
          	<option>AL</option>
            <option>AK</option>
            <option>AZ</option>
            <option>AR</option>
            <option>CA</option>
            <option>CO</option>
            <option>CT</option>
            <option>DE</option>
            <option>FL</option>
            <option>GA</option>
            <option>HI</option>
            <option>ID</option>
            <option>IL</option>
            <option>IN</option>
            <option>IA</option>
            <option>KS</option>
            <option>KY</option>
            <option>LA</option>
            <option>ME</option>
            <option>MD</option>
            <option>MA</option>
            <option>MI</option>
            <option>MN</option>
            <option>MS</option>
            <option>MO</option>
            <option>MT</option>
            <option>NE</option>
            <option>NV</option>
            <option>NH</option>
            <option>NJ</option>
            <option>NM</option>
            <option>NY</option>
            <option>NC</option>
            <option>ND</option>
            <option>OH</option>
            <option>OK</option>
            <option>OR</option>
            <option>PA</option>
            <option>RI</option>
            <option>SC</option>
            <option>SD</option>
            <option>TN</option>
            <option>TX</option>
            <option>UT</option>
            <option>VT</option>
            <option>VA</option>
            <option>WA</option>
            <option>WV</option>
            <option>WI</option>
            <option>WY</option>
          </select>
          <br />
          <br />
          </fieldset>
          <br />
          <fieldset>
          <br />
          <input type="submit" name="submit" value="Submit Info" />&nbsp;<input type="reset" name="reset" /><br />
          <br />
          </fieldset>
        </form>
    The PHP script is as follows:

    Code:
    <?php
    $con = mysql_connect('localhost', 'user1', 'password1');
    mysql_select_db('yucha', $con) or die( "Unable to select database");
    if(empty($_GET["LastName"]) || empty($_GET["FirstName"]) || empty($_GET["State"])) {
    echo "<div id=\"area1\">Missing DATA<br /></div>";
    }
    else {
    $search = $_GET["LastName"] || $_GET["FirstName"] || $_GET["State"];
    $result = mysql_query("SELECT * FROM 'telephone_directory' WHERE * ID = '$search' ");
    echo "<table cols=\"3\" align=\"center\">";
    echo "<tr><th>Name</th><th>State</th><th>Number</th>";
    while ($row = mysql_fetch_array($result)) {
    $Last_Name = $row['LastName'];
    $First_Name = $row['FirstName'];
    $State = $row['State'];
    echo "<tr>";
    echo "<td>".$row['Last_Name']. ", " . $row['First_Name'] . "</td>" . "<td>" . $row['State'] . "</td>" . "<td>" . $row['AreaCode'] . "-" . $row['Prefix'] . "-" . $row['Number'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    }
    mysql_close($con);
    ?>
    I keep getting the following error message:

    Notice: Undefined index: LastName in C:\wamp\www\SelectDirectory.php on line 47

    Notice: Undefined index: FirstName in C:\wamp\www\SelectDirectory.php on line 47

    Notice: Undefined index: State in C:\wamp\www\SelectDirectory.php on line 47
    Name State Number
    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\SelectDirectory.php on line 51



    Now I am a rookie when it comes to PHP and I have only been working with this scripting for 5 weeks total, so any help, suggestions, or pointers in the right direction would be greatly appreciated.
    Last edited by StHelensBull; 12-30-2010 at 03:05 PM. Reason: CODE

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Make sure after every query you do or die(mysql_error());
    PHP Code:
    $result mysql_query("SELECT * FROM 'telephone_directory' WHERE * ID = '$search' ") or die(mysql_error()); 
    When pasting a code, please remember to use [code] tags. That includes [ html ], [ code ], and [ php ].
    Jeremy | jfein.net

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I would like to thank absolutely no one for giving any help on this.

    St Helens Bull

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm a bit of a rookie myself when it comes to PHP and completely lost with MySQL. But those seem like pretty straightforward errors. However, there's no line 47 or line 51 in the code from your post.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •