Forum Moderators: coopster

Message Too Old, No Replies

PHP and MySQL

Retrieve Data and Display

         

pancgom

4:55 pm on Apr 5, 2008 (gmt 0)

10+ Year Member



Hi,

I have a request which i have been posting but without no help. I am very very new to php and mysql i have a mysql table with three columns name. empID and department.

What i am trying to do is to populate a list menu in a php page with the name's in the mysql table. Once the name is selected i hit a submit button which will return the other details on the table i.e., empID and department corresponding to the name.

I know this sounds basic but i have no clue of how to do this. I know i should be trying to do this by learning it, but i am still learning php and i need this really urgent. Can someone please help me with the code. I have been turned down by other forums asking me to try myself but i dont have the time atleast for this weekend because i have to get this up on monday along with other stuff to be done on the form that i am building.

Thanks a lot for any help.

coopster

7:57 pm on Apr 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, pancgom.

There are some good resources in our PHP Forum Library [webmasterworld.com] to get you started such as the Basics of extracting data from MySQL using PHP [webmasterworld.com].

pancgom

10:08 am on Apr 6, 2008 (gmt 0)

10+ Year Member



hi,

I tried the code at this page

[webmasterworld.com...]

which more or less suits my need changed all of the details needed for my form but i am not able to figure out the last part where it says

// Your table to display the output of your query goes in here

i tried the code in the reply as well but not working, the drop down is able to retrieve the name and display but the other details are not coming up.

Here is something that i tried, sorry me not good at this guys here is what i did replacing the // Your table to display the output of your query goes in here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hr Leave Application Form</title>
<script language="JavaScript" src="ts_picker.js">
//Script by Denis Gritcyuk: tspicker@yahoo.com
//Submitted to JavaScript Kit (http://javascriptkit.com)
//Visit [javascriptkit.com...] for this script
</script>
</head>

<body>

<?php

$db_host = "localhost";
$db_user = "#*$!x";
$db_pwd = "#*$!x";
$db_name = "#*$!x";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

if(!isset($_POST['Name'])){
$Var ='%';
}
else
{
if($_POST['Name'] == "ALL"){
$Var = '%';
} else {
$Var = $_POST['Name'];
}
}

//THIS PUTS A FORM WITH A DROP DOWN BOX TO SELECT THE VARIETY, AND SHOW THE VARIETY IF ALREADY SELECTED

echo "<form action=\"HRFormTRY4_8.php\" method=\"POST\">
<table border=0 cellpadding=5 align=center><tr><td><b>Name</b><br>";

$res=mysql_query("select * from PersonnelDetails");
if(mysql_num_rows($res)==0){
echo "there is no data in table..";
} else {
echo "<select Name=\"Name\" id=\"Name\"><option value=\"ALL\">ALL</option>";
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option value=$row[Name]";
if($Var==$row[Name])
echo " selected";
echo ">$row[Name]</option>";
}

echo "</select>";
}

echo "</td><td align=\"center\"><input type=\"submit\" value=\"Search Now\" />
</td></tr></table></form><br>";

//THIS TAKES THE VARIETY YOU’VE SELECTED, QUERIES THE DATABASE FOR ALL THE RQUIRED INFO AND OUTPUTS YOUR RESULTS

$query = "SELECT * FROM PersonnelDetails WHERE Name LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){
echo("This name does not match your currently selected name. Please try another name!");
} ELSE {

//this is what i inserted from the other post

echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>Column 1 Name</h3></td> <td><H3>Column 2 Name</H3></td> <td><H3>Column 3 Name</H3></td><td><H3>Column 4 Name</H3></td></tr>";
// keeps getting the next row until there are no more to get
//while ($row = mysql_fetch_array($result)) {

// Print out the contents of each row into a table
echo "</td><td>";
echo $row['StaffNo'];
echo "</td><td>";
echo $row['Department'];
echo "</td><td>";
echo $row['PresentAppointment'];
echo "</td></tr>";
//}
echo "</table>";

mysql_free_result($result);

}
?>
</body>
</html>

Can someone help me with this please.

cameraman

4:53 pm on Apr 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is it displaying where the table shows - anything?
This line here:
echo"<option value=$row[Name]";
Try adding quotes:
echo"<option value=\"$row[Name]\"";

If that doesn't do anything, where this is:
if($_POST['Name'] == "ALL"){
$Var = '%';
} else {
$Var = $_POST['Name'];
}

Just for debugging purposes, echo the name:
if($_POST['Name'] == "ALL"){
$Var = '%';
} else {
$Var = $_POST['Name'];
echo "<p>Selected $Var</p>";
}

You can also try setting it to a specific name that you know exists in the table (again, just for debugging):

//THIS TAKES THE VARIETY YOU’VE SELECTED, QUERIES THE DATABASE FOR ALL THE RQUIRED INFO AND OUTPUTS YOUR RESULTS

$query = "SELECT * FROM PersonnelDetails WHERE Name LIKE '$Var' ";


Change the last line to:
$query = "SELECT * FROM PersonnelDetails WHERE Name = 'Bob Jones' ";

Report your results.

[edited by: tedster at 6:34 am (utc) on April 7, 2008]
[edit reason] fix formatting problem [/edit]

pancgom

6:09 am on Apr 7, 2008 (gmt 0)

10+ Year Member



Hi,

The name from the databases is getting displayed, but the last part where the information corresponding to the name that should be displayed is the one that is not working.

Please see below the code to retrieve the data from the database.

The while loop actually drags all the content from the database and displays along with the drop down box with the Name's form the database.

What i want is how to select the name in the drop down box and then click submit then it will display the details corresponding to the Name selected.

Thanks a lot for you help.

//THIS TAKES THE Name YOU’VE SELECTED, QUERIES THE DATABASE FOR ALL THE REQUIRED INFO AND OUTPUTS YOUR RESULTS

$query = "SELECT * FROM PersonnelDetailsInterdisciplinaryTheory WHERE Name LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){
echo("This name does not match your currently selected name. Please try another name!");
} ELSE {

echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>Column 1 Name</h3></td> <td><H3>Column 2 Name</H3></td> <td><H3>Column 3 Name</H3></td><td><H3>Column 4 Name</H3></td></tr>";
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)) {

// Print out the contents of each row into a table
echo "</td><td>";
echo $row['StaffNo'];
echo "</td><td>";
echo $row['Department'];
echo "</td><td>";
echo $row['PresentAppointment'];
echo "</td></tr>";
//}
echo "</table>";

mysql_free_result($result);

}
?>

[edited by: eelixduppy at 6:24 am (utc) on April 7, 2008]

cameraman

7:51 am on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I understand that.
Now that you've uncommented the while, you should be getting an error because you didn't uncomment its matching closing brace (at least in the snippet you posted).

Did you try any of the things I suggested? What was the outcome, any change?

Without quotes around the value attributes of your option elements, the browser may not send the correct data back to the server (for example if it's a full name like Bill Smith it may only send Bill, and it would likely be browser dependent).

If you view html source is there anything at all where the table's supposed to be? I notice now that you're not outputting any <tr> elements in your while, so the data may be there and the table just isn't rendering correctly.

pancgom

8:42 am on Apr 7, 2008 (gmt 0)

10+ Year Member



Amazing it worked.... It worked ...It worked...

cameraman

8:46 am on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, so add an echo here:
$query = "SELECT * FROM PersonnelDetailsInterdisciplinaryTheory WHERE Name LIKE '$Var' ";
echo $query;
$result = mysql_query($query) or die("Error: " . mysql_error());

and see what name it's looking for - if it's not the name you're selecting, there's something wrong with the submission - probably the quotes. Once you've got the name straightened out, you may want to change it to Name = '$Var' instead of using LIKE.

pancgom

8:52 am on Apr 7, 2008 (gmt 0)

10+ Year Member



It worked...i did what u said... i was missing a quote...finally it worked..thank you thank you thank you....

cameraman

8:55 am on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool!