php10a.php
<html>
<head>
<style type="text/css">
#mainform, #subform {
background: lightgreen;
margin-left: 500px;
margin-right: 500px;
}
</style>
</head>
<body bgcolor="pink">
<center>
<h2>Student-Details</h2>
<h2>Registration Form</h2>
<div id="mainform">
<form method="post" action="pgm10b.php">
Roll No:<input type="text" name="t0" /><br><br>
Name:<input type="text" name="t1" /><br><br>
Address Line 1:<input type="text" name="t2"><br><br>
Address Line 2:<input type="text" name="t3"><br><br>
Email-ID:<input type="text" name="t4"><br><br>
<input type="submit" value="Insert" />
<input type="reset" value="Reset" /><br><br>
</form>
</div>
<br><br>
<h2>Search Info</h2>
<div id="subform">
<form method="post" action="pgm10c.php">
Name:<input type="text" name="t5" /><br><br>
<input type="submit" value="Search" />
<input type="reset" value="Reset" /><br><br>
</form>
</div>
</center>
</body>
</html>
php10b.php
<?php
# Take inputs & store in local variables
$rno = $_POST['t0'];
$name = $_POST['t1'];
$addr1 = $_POST['t2'];
$addr2 = $_POST['t3'];
$email = $_POST['t4'];
try {
# Connect to the database
$con = mysqli_connect('localhost', 'root', '') or die ('Could not connect');
mysqli_select_db($con, "bca5") or die ('Could not select database');
# Insert query
$query = "insert into student values('$rno', '$name', '$addr1', '$addr2', '$email')";
$res = mysqli_query($con, $query);
if ($res) {
echo "Data inserted successfully!";
} else {
echo "Error in insertion!";
}
mysqli_close($con);
} catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
?>
php10c.php
<html>
<body bgcolor="#ffe6e6">
<h2 align="center">Search Result</h2>
<?php
$rn = $_POST['t5'];
try {
# Connect to the database
$con = mysqli_connect('localhost', 'root', '') or die ('Could not connect');
mysqli_select_db($con, "bca5") or die ('Could not select database');
# Search query
$result = mysqli_query($con, "select * from student where rno='$rn'");
if (mysqli_num_rows($result) == 0) {
echo "$rn doesn't exist";
exit();
}
# Display results in a table
echo "<table border=1 bgcolor=#cce6ff align=center>";
echo "<tr>";
echo "<th>Roll No</th><th>Name</th><th>Address Line 1</th><th>Address Line 2</th><th>Email-ID</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['rno']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['addr1']."</td>";
echo "<td>".$row['addr2']."</td>";
echo "<td>".$row['email']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
} catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
?>
</body>
</html>
0 Comments