First, we will introduce how to query data by passing values in a form.
Task target: Enter the Department name in the form to query the personnel information of the corresponding department.
Create a search. php file first.
Step 1: Insert a form that contains an input box and a submit button. The content of the search. php file is as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
</Head>
<Body>
<H3> Search
<Form action = "search_result.php" method = "POST">
Depart Name: <input type = "text" size = 25 name = "depart" value = ""> <br>
<Input type = "submit" name = "submit" value = "Search">
</Form>
</Body>
</Html>
In this way, we get a data submission page, which means that when we click the Search button, the system passes the data entered in the input box named depart to the search_result.php file through the Post method.
Step 2: Since we have already transferred the value, we need to create another page file to receive the value. Because it has already been specified to be transferred to search_result.php, we will create a file named search_result.php.
In this file, First connect to the database and select the Data source:
Copy codeThe Code is as follows:
<? Php
$ Link = mysql_connect ("localhost", "root", "previous administrator password ");
If (! $ Link) echo "no connection successful! ";
Else echo "connection successful! ";
Mysql_select_db ("infosystem", $ link );
?>
Then, the system receives the parameters from the search. php file and generates the SQL query statement:
Copy codeThe Code is as follows:
<? Php
$ Depart = $ _ POST ["depart"];
$ Q = "SELECT * FROM info where depart = '$ depart '";
?>
Finally, execute the SQL statement and display the data:
Copy codeThe Code is as follows:
<? Php
Mysql_query ("set names GB2312 ");
$ Rs = mysql_query ($ q, $ link );
Echo "<table> ";
Echo "<tr> <td> Department </td> <td> name </td> </tr> ";
While ($ row = mysql_fetch_object ($ rs )) echo "<tr> <td> $ row-> depart </td> <td> $ row-> ename </td> </tr> ";
Echo "</table> ";
Mysql_close ($ link );
?>
After querying, do you get the data you need? Of course, this is just the most basic example. In the next few topics, I will continue to explain how to query the data.