Log in

View Full Version : Extracting Data From MySQL using php



StHelensBull
12-27-2010, 08:22 AM
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:


<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:


<?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.

Nile
12-28-2010, 01:03 AM
Make sure after every query you do or die(mysql_error());


$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 ].

StHelensBull
12-31-2010, 11:59 PM
I would like to thank absolutely no one for giving any help on this.

St Helens Bull

jscheuer1
01-01-2011, 08:03 AM
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.