Lookup and Display Data from Mysql Database

Codes here !

Moderators: macek, egami, gesf

meesh2175
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Feb 16, 2012 1:03 pm

Lookup and Display Data from Mysql Database

Postby meesh2175 » Wed Aug 22, 2012 6:53 am

Hello, I am new to PHP and I could really use some help!

I have been tasked with creating an online form to track employee training. I have created a database with a table which includes 5 fields (id, lname, fname, dept and status). I have uploaded all of my employee information into the table.

I would like it so that my online form (see attached picture) will look up their ID# in the database and then display their first name, last name, department and status in the corresponding columns.

I honestly don't even know where to start. I know how to connect to my database but that is about it.

Thanks!!!
Michelle
Attachments
Clipboard02.png
Clipboard02.png (11.18 KiB) Viewed 338 times

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Lookup and Display Data from Mysql Database

Postby johnj » Wed Aug 22, 2012 9:21 pm

Add a submit button at the end of the form(use the form tag at the appropriate place). When the user enters the ID# (one or many) and clicks submit, get all ID# in the form, connect to the database, fetch all details for each employee using ID# and display it.

When you have completed the above and comfortable with the coding, remove the submit button and use jquery/ajax so that as soon as the user completes typing the ID#, the corresponding details are fetched and displayed without the user clicking the submit button.

meesh2175
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Feb 16, 2012 1:03 pm

Re: Lookup and Display Data from Mysql Database

Postby meesh2175 » Thu Aug 23, 2012 4:52 am

johnj wrote:Add a submit button at the end of the form(use the form tag at the appropriate place). When the user enters the ID# (one or many) and clicks submit, get all ID# in the form, connect to the database, fetch all details for each employee using ID# and display it.

When you have completed the above and comfortable with the coding, remove the submit button and use jquery/ajax so that as soon as the user completes typing the ID#, the corresponding details are fetched and displayed without the user clicking the submit button.


Thanks johnj for your help!! I do have a submit button, I must of cropped it out of my picture. My issue is I don't know how to look up the details for more than one ID# at a time. I can fetch and display the details for one employee at a time but can't look up all 10. Any suggestions? Thanks again!

meesh2175
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Feb 16, 2012 1:03 pm

Re: Lookup and Display Data from Mysql Database

Postby meesh2175 » Thu Aug 23, 2012 5:53 am

I figure it out! For anyone who looking for something similar, here is my my small sample code.

Code: Select all

<?php
//Connect and select database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

//Set the variables
$search1 = $_POST["id1"];
$search2 = $_POST["id2"];

//Search the first ID and display results
$data = mysql_query("SELECT * FROM table_name WHERE id LIKE '$search1'") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
   echo "".$info['id'] . "  ";
   echo "".$info['lname'] . " ";
   echo "".$info['fname'] . "  ";
   echo "".$info['dept'] . "  ";
   echo "".$info['status'] . "<br><br>";
}

//Search the second ID and display results
$data2 = mysql_query("SELECT * FROM table_name WHERE id LIKE '$search2'") or die(mysql_error());
while($info2 = mysql_fetch_array( $data2 ))
{
   echo "".$info['id'] . "  ";
   echo "".$info['lname'] . " ";
   echo "".$info['fname'] . "  ";
   echo "".$info['dept'] . "  ";
   echo "".$info['status'] . "<br><br>";
}

?>


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest