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
Lookup and Display Data from Mysql Database
Moderators: egami, macek, gesf
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.
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!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.
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>";
}
?>