SoFunction
Updated on 2025-04-09

Passing parameter instance code between PHP pages

 
First, let me introduce to you how to query data through the form to pass the value.

Task objective: Enter the department name in the form to query the personnel information of the corresponding department.

Create the file first.

The first step is to insert a form that contains an input box and a submit button. The content of the file is as follows:

Copy the codeThe code is as follows:

<html> 
<head> 
</head> 
<body> 
<h3>Search</h3> 
<form action="search_result.php" method="POST"> 
Depart Name:<input type="text" size=25 name="depart" value=""><br><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 sent out the value before, we have created another page file to receive this value. Because it has been specified before to send it to search_result.php, then we will create a new file named search_result.php.

In this file, first connect to the database and select the data source:

Copy the codeThe code is as follows:

<?php  
$link=mysql_connect("localhost","root","previous administrator password");
if(! $link) echo "No connection was successful!";
else echo "Connected successfully!";
mysql_select_db("infosystem", $link); 
?> 

Secondly, receive the parameters issued by the file and generate SQL query statement:

Copy the 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 the 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); 
?> 

Through query, do you get the data you need? Of course, this is just the most basic example. I will continue to query data for the next few topics.