Slip 28 - C) Consider the following entities and their relationships student (sno integer, s_name char(30), s_class char(10), s_addr char(50)), teacher (tno integer, t_name char (20), qualification char (15),experience integer). The relationship between student-teacher: m-m with descriptive attribute subject. Using above database write a script in PHP to accept a teacher name from user and display the names of students along with subjects to whom teacher is teaching.

Solution:

<html>

<body>

<form action="s28.php" method="POST">

<table border=1>

<tr>

<td>Enter teacher's name : </td>

<td> <input type="text" name="tname"></td>

</tr>

<tr><td></td><td><input type="submit" name="submit" value="Submit"></td></tr>

</form>

</body>

</html>

S28.php

<?php

$database="archana2";

$con =mysqli_connect("localhost","root" ,"");

if (!$con)

{

die('Could not connect: ' . mysqli_error());

}

mysqli_select_db ($con,"$database");

$tname=$_POST['tname'];

echo $query="select sname,subject from student,s_t,teacher where student.sno=s_t.sno and teacher.tno=s_t.tno  and tname='".$tname."'";

$result=mysqli_query($con,$query);

if(!$result)

{

die("An error occured...");

}

echo '<table>

<tr>

<th>Sname</th>

<th>Subject</th>

</tr>';

while($row=mysqli_fetch_array($result))

{  

echo'

<tr>

<td>'.$row[0].'</td>

<td>'.$row[1].'</td>

</tr>';

}

echo '</table>';

mysqli_close($con);

?>

Post a Comment

0 Comments