Search results in MySQL database using PHP
Search results in MySQL database using PHP
HTML
<form action="test1.php" method="POST">
<table>
<tr>
<th>Add Data to Student Table</th>
</tr>
<tr>
<td><label for="id">Stident ID: </label></td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td><label for="name">Student Name: </label></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><label for="tele">Student Phone Number: </label></td>
<td><input type="text" name="tele"></td>
</tr>
<tr>
<td><label for="address">Student Address: </label></td>
<td><input type="text" name="address" id="address"></td>
</tr>
</table>
<br>
<input type="submit" name="update">
</form>
PHP for Update Values
<?php
if(isset($_POST['update'])){
$connection = mysqli_connect("localhost","root","root","student_info", 8000);
if(!$connection){
die("Error");
}else{
echo "Successsfully Connected";
$sql = "INSERT INTO student(St_ID, St_Name, St_Phone, St_Address)
VALUES(
'$_POST[id]',
'$_POST[name]',
'$_POST[tele]',
'$_POST[address]'
)";
}
if(!mysqli_query($connection, $sql)){
die("Error");
}else{
echo "<br>";
echo "Updated Successfully";
}
mysqli_close($connection);
}
?>
HTML to Search Values
<h2>Search Details of the Students</h2>
<div>
<form action="test2.php" method="POST">
<table>
<tr>
<td><p>Search by Student ID: </p></td>
<td><input type="text" name="stId"></td>
<td><input type="submit" value="Check"></td>
</tr>
</table>
</form>
</div>
HTML UX View
| HTML Output of Two Forms |
PHP to Search Values
<?php
$connection = mysqli_connect("localhost","root","root","student_info", 8000);
$id = $_POST['stId'];
$qry = "SELECT * FROM student WHERE St_ID = $id";
$result = mysqli_query($connection, $qry);
$details = mysqli_fetch_array($result);
mysqli_free_result($result);
echo "ID: ". $details['St_ID'];
echo "<br>Name: ". $details['St_Name'];
echo "<br>Phone Number: ".$details['ST_Phone'];
echo "<br>Adress: ".$details['St_Address'];
?>
Output of the Search
| Search Result |
To understand well, just go through the following codes, All in one file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="test1.php" method="POST">
<table>
<tr>
<th>Add Data to Student Table</th>
</tr>
<tr>
<td><label for="id">Stident ID: </label></td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td><label for="name">Student Name: </label></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><label for="tele">Student Phone Number: </label></td>
<td><input type="text" name="tele"></td>
</tr>
<tr>
<td><label for="address">Student Address: </label></td>
<td><input type="text" name="address" id="address"></td>
</tr>
</table>
<br>
<input type="submit" name="update">
</form>
<div>
<h2>Search Details of the Students</h2>
<div>
<form action="test1.php" method="POST">
<table>
<tr>
<td><p>Search by Student ID: </p></td>
<td><input type="text" name="stId"></td>
<td><input type="submit" value="Check" name="checkID"></td>
</tr>
<tr>
<td><p>Search by Student Name: </p></td>
<td><input type="text" name="stName"></td>
<td><input type="submit" value="check" name="checkName"></td>
</tr>
<tr>
<td><p>Search by Student Address: </p></td>
<td><input type="text" name="stAddress"></td>
<td><input type="submit" value="check" name="checkAddress"></td>
</tr>
</table>
</form>
</div>
</div>
<?php
if(isset($_POST['update'])){
$connection = mysqli_connect("localhost","root","root","student_info", 8000);
if(!$connection){
die("Error");
}else{
echo "Successsfully Connected";
$sql = "INSERT INTO student(St_ID, St_Name, St_Phone, St_Address)
VALUES(
'$_POST[id]',
'$_POST[name]',
'$_POST[tele]',
'$_POST[address]'
)";
}
if(!mysqli_query($connection, $sql)){
die("Error");
}else{
echo "<br>";
echo "Updated Successfully";
}
mysqli_close($connection);
} elseif(isset($_POST['checkID'])){
$connection = mysqli_connect("localhost","root","root","student_info", 8000);
$id = $_POST['stId'];
$qry = "SELECT * FROM student WHERE St_ID = $id";
$result = mysqli_query($connection, $qry);
$details = mysqli_fetch_array($result);
mysqli_free_result($result);
echo "ID: ". $details['St_ID'];
echo "<br>Name: ". $details['St_Name'];
echo "<br>Phone Number: ".$details['ST_Phone'];
echo "<br>Adress: ".$details['St_Address'];
} elseif(isset($_POST['checkName'])){
$connection = mysqli_connect("localhost","root","root","student_info", 8000);
$name = $_POST['stName'];
$qry = "SELECT * FROM student WHERE St_Name = '$name'";
$result = mysqli_query($connection, $qry);
$details = mysqli_fetch_array($result);
mysqli_free_result($result);
echo "ID: ". $details['St_ID'];
echo "<br>Name: ". $details['St_Name'];
echo "<br>Phone Number: ".$details['ST_Phone'];
echo "<br>Adress: ".$details['St_Address'];
} elseif(isset($_POST['checkAddress'])){
$connection = mysqli_connect("localhost","root","root","student_info", 8000);
$address = $_POST['stAddress'];
$qry = "SELECT * FROM student WHERE St_Address = '$address'";
$result = mysqli_query($connection, $qry);
$details = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
$count = 1;
foreach($details as $detail){
echo "<h2><br>Student $count Details</h2>";
echo "ID: ". $detail['St_ID'];
echo "<br>Name: ". $detail['St_Name'];
echo "<br>Phone Number: ".$detail['ST_Phone'];
echo "<br>Adress: ".$detail['St_Address'];
$count += 1;
}
}
?>
</body>
</html>
For more information,
Thank You
Thank you!
ReplyDelete