Hi All,
Searching is most important in any student project.
This requires 5 important steps. [You can skip step 1,2 and 3 if you already have Database, table and data ]
1. Create Database - SNIT
Query : CREATE DATABASE SNIT
2. Create Table - student
Query : CREATE TABLE student(name TEXT(20),rollno int(3),marks int(3))
3. Insert few records in the table
Query : INSERT INTO `SNIT`.`student` (`name`, `rollno`, `marks`) VALUES ('Raju', '18', '90'), ('Babu', '9', '81')
4. Design Search form - save it as searchform.html
<html>
<body>
<form name=frmsearch action=searchstud.php method=post>
Name <input type=text name=sname><input type=submit value='Search Student'>
</form>
</body>
</html>
5. Write PHP program to search and display records. - save it as searchstud.php
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("SNIT",$con);
$sname=$_POST['sname'];
$sql="Select * from student where name like '%$sname%'";
echo $sql;
$data=mysql_query($sql,$con);
echo "<table border=1 align=center width=50%>";
echo "<tr><th>Name</th><th>Roll No</th><th>Marks</th></tr>";
while($row=mysql_fetch_array($data))
{
$name=$row['name'];
$rollno=$row['rollno'];
$marks=$row['marks'];
echo "<tr><td>$name</td><td>$rollno</td><td>$marks</td></tr>";
}
mysql_close($con);
?>
NB: Also try the same code with other table containing more fields
All the best.
Searching is most important in any student project.
This requires 5 important steps. [You can skip step 1,2 and 3 if you already have Database, table and data ]
1. Create Database - SNIT
Query : CREATE DATABASE SNIT
2. Create Table - student
Query : CREATE TABLE student(name TEXT(20),rollno int(3),marks int(3))
3. Insert few records in the table
Query : INSERT INTO `SNIT`.`student` (`name`, `rollno`, `marks`) VALUES ('Raju', '18', '90'), ('Babu', '9', '81')
4. Design Search form - save it as searchform.html
<html>
<body>
<form name=frmsearch action=searchstud.php method=post>
Name <input type=text name=sname><input type=submit value='Search Student'>
</form>
</body>
</html>
5. Write PHP program to search and display records. - save it as searchstud.php
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("SNIT",$con);
$sname=$_POST['sname'];
$sql="Select * from student where name like '%$sname%'";
echo $sql;
$data=mysql_query($sql,$con);
echo "<table border=1 align=center width=50%>";
echo "<tr><th>Name</th><th>Roll No</th><th>Marks</th></tr>";
while($row=mysql_fetch_array($data))
{
$name=$row['name'];
$rollno=$row['rollno'];
$marks=$row['marks'];
echo "<tr><td>$name</td><td>$rollno</td><td>$marks</td></tr>";
}
mysql_close($con);
?>
NB: Also try the same code with other table containing more fields
All the best.
No comments:
Post a Comment