Sunday, July 9, 2017

3.Write a PHP program to read student data from an XML file and store into the MySQL database. Retrieve and display using SEARCH function.

lab3.xml

<?xml version="1.0" ?>
<student>
<stud>
<name>BBBB</name>
<usn>1234</usn>
</stud>
<stud>
<name>CCCC</name>
<usn>4567</usn>
</stud>
</student>

lab3.html

<html>
<body>
<center>
<table width="50%" border="2"><tr>
<form action="http://localhost/lab3.php">
<tr><th> ENTER NAME TO SEARCH </th></tr><br/>
<td align="center"><br/>Name: <input type="text" name="name"><br /><br/>
submit: <input type="submit"><br/><br/></td>
</form></tr>
</body>
</html>

lab3.php

<?php
$link=mysqli_connect("localhost","root","","test");
if(!$link)
{
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$name=$_GET['name'];
$studs=simplexml_load_file("http://localhost/lab3.xml");
foreach($studs as $stud)
{
$name = $stud->name;
$usn = $stud->usn;
mysqli_query($link,"insert into stud3 values('$name','$usn')");
}
$name=$_GET['name'];
$res=mysqli_query($link,"select * from stud3 where name='$name'");
if(mysqli_num_rows($res)==0)
{
echo "$name doesn't exist in the table";
}
else
{
echo "<table border='1' align=center width=60%>";
echo "<tr><td width=15% align=center>NAME</td><td width=15%
align=center>USN</td></tr>";
while($arr=mysqli_fetch_row($res))
echo "<tr><td>$arr[0]</td><td>$arr[1]</td></tr>";
echo "</table>";
}
?>

7 comments: