1) Create a XML file which gives details of
books available in “ABC Bookstore” from following categories
1) Technical 2) Cooking 3) YOGA
abcbook.xml
<?xml version="1.0"?>
<ABCBOOK>
<Technical>
<BOOK>
<Book_PubYear>ABC</Book_PubYear>
<Book_Title>pqr</Book_Title>
<Book_Author>400</Book_Author>
</BOOK>
</Technical>
<Cooking>
<BOOK>
<Book_PubYear>BBC</Book_PubYear>
<Book_Title>pPr</Book_Title>
<Book_Author>500</Book_Author>
</BOOK>
</Cooking>
<Yoga>
<BOOK>
<Book_PubYear>CBC</Book_PubYear>
<Book_Title>pRr</Book_Title>
<Book_Author>600</Book_Author>
</BOOK>
</Yoga>
</ABCBOOK>
abcbook.php
<?php
$xml=simplexml_load_file("abcbook.xml") or die("cannnot
load");
$xmlstring=$xml->asXML();
echo $xmlstring;
?>
2)Write a PHP program to implement Create, Read,Update and Display operations on Teacher table with attributes(tid, tname,address, subject). (Use Radio Buttons)
Slip18.html
<html>
<head>
<script>
function showStuff(showid){
var element =
document.getElementsByClassName('hide_item');//.style.display = 'none';
for(var i=0; i<element.length; i++)
{
element[i].style.display = 'none';
}
document.getElementById(showid).style.display =
'block';
}
</script>
</head>
<body>
<form method="post"
action="slip18.php">
<input type="radio" name="act" id="create"value="create" onClick="showStuff('teacher_details');">create
<input type="radio" name="act"
id="read" value="read"
onClick="showStuff('add_teacher');">read
<input type="radio" name="act"
id="update" value="update" onClick="showStuff('update_teacher');">update
<input type="radio" name="act"
id="display" value="display"
onClick="showStuff('display_teacher');">display
<div id="update_teacher"
style="display:none;" class="hide_item">
<p><b>teacher
Details:</b></p>
<b>tname<input type="text"
name="tname" />
<input type="submit"
name="submit" value="update" />
</div>
<div id="teacher_details"
style="display:none;" class="hide_item">
<p><b> Create table
Teacher:</b></p>
<input type="submit"
name="submit" value="Create" />
</div>
<div id="add_teacher"
style="display:none;" class="hide_item">
<p><b>Teacher
Details:</b></p>
<b>tid</b><input
type="text" name="tid" />
<b>tname</b><input
type="text" name="tname1" />
<b>address</b><input
type="text" name="address" />
<b>subject</b><input
type="text" name="subject" />
<input type="submit"
name="submit" value="Read" />
</div>
<div id="display_teacher"
style="display:none;" class="hide_item">
<p><b>Display All Teacher
Details:</b></p>
<input type="submit"
name="submit" value="display" />
</div>
</form>
</body>
</html>
Slip18.php
<?php
$database="archana"; //database name
$con
=@mysql_connect("localhost","root" ,"");//for
wamp 3rd feild is balnk
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$database", $con);
if($_POST['submit'] == 'Create')
{
mysql_query("create table teacher (tno int
primary key,tname varchar(10),address varchar(10),subject varchar(10))")
or die(mysql_error());
echo "Sucessfuly created table";
}
if($_POST['submit'] == 'Read')
{
$tid=$_POST['tid'];//this values comes from html
file after submitting
$tname=$_POST['tname1'];
$address = $_POST['address'];
$subject = $_POST['subject'];
$con=@mysql_connect("localhost","root"
,"");//for wamp 3rd feild is balnk
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$database", $con);
$query = "INSERT INTO teacher
(tno,tname,address,subject)VALUES ('$tid','$tname','$address','$subject')";
mysql_query($query) or die(mysql_error());
//$mno = mysql_insert_id(); .
echo "<script
type='text/javascript'>\n";
echo "alert('you are Succesflly added
record');\n";
echo "</script>";
}
if($_POST['submit'] == 'update')
{
$q=$_POST["tname"];
$con
=@mysql_connect("localhost","root" ,"");//for
wamp 3rd feild is balnk
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$database", $con);
//echo "update teacher set address='PUNE' where
tname='".$q."'";
$result1=mysql_query("update teacher set
address='PUNE' where tname='".$q."'") or die(mysql_error());
$result=mysql_query("SELECT * from teacher
where tname='".$q."'") or die(mysql_error());
echo"<table border='1'>";
echo"<tr><th>tid</th><th>tname</th><th>address</th><th>subject</th></tr>";
while($row=mysql_fetch_array($result))
{
echo"<tr><td>";
echo $row['tno'];
echo"</td><td>";
echo $row['tname'];
echo"</td><td>";
echo $row['address'];
echo"</td><td>";
echo $row['subject'];
echo"</td></tr>";
}
echo"</table>";
}
if($_POST['submit'] == 'display'){
//$q=$_POST["tname"];
$con
=@mysql_connect("localhost","root" ,"");//for
wamp 3rd feild is balnk
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$database", $con);
$result=mysql_query("SELECT * from
teacher");
echo"<table border='1'>";
echo"<tr><th>tid</th><th>tname</th><th>address</th><th>subject</th></tr>";
while($row=mysql_fetch_array($result))
{
echo"<tr><td>";
echo $row['tno'];
echo"</td><td>";
echo $row['tname'];
echo"</td><td>";
echo $row['address'];
echo"</td><td>";
echo $row['subject'];
echo"</td></tr>";
}
echo"</table>";
}
mysql_close($con);
?>
Another solution
slip_18.html
<html>
<body>
<form action="slip_18.php"
method="get">
<center> <h2> Teachers Details
:</h2> <br>
<table>
<tr> <td><input type=radio
name=r value="1"></td>
<td><b>Create Table:</b></td> </tr>
<tr> <td><input type=radio
name=r value="2"></td> <td><b>
Read/Insert Values :</b></td> </tr>
<tr> <td><input type=radio
name=r value="3"></td>
<td><b>Update Values :</b></td> </tr>
<tr> <td><input type=radio
name=r value="4"></td>
<td><b>Display :</b></td> </tr>
</table>
<br> <input type=submit
value=Submit name=submit>
</center>
</form>
</body>
</html>
slip_18.php
<?php
$r=$_GET['r'];
$con=@mysql_connect("localhost","root","");
$d=mysql_select_db("archana",$con);
if($r==1)
{
$q=mysql_query("create table teachers(tid
int,tname varchar(10),address varchar(20),subject varchar(10))");
echo "<center><b><h2>
Table Created Successfully.....!!!</h2><b></center>";
}
else if($r==2)
{
header("location:slip_18_insert.html");
}
else if($r==3)
{
header("location:slip_18_update.php");
}
else if($r==4)
{
$q4=mysql_query("select *from
teachers");
echo "<center>";
echo "<table border=1 width=30%
height=20%>";
echo
"<h2><tr><td><b>Tid
</b></td><td> <b>TName<b> </td><td>
<b>address <b></td><td> <b>Subject
<b></td></tr></h2>";
while($row=mysql_fetch_array($q4))
{
echo
"<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td></tr>";
}
echo "</table>";
echo "</center>";
}
?>
slip_18_insert.html
<html>
<body>
<form action="slip_18_insert.php"
method="get">
<center> <h2>Enter Details
:</h2> <br>
<table>
<tr> <td>Enter no
:</td><td><input type=text name=a_no> </td> </tr>
<tr> <td>Enter name
:</td><td><input type=text name=a_name></td>
</tr>
<tr <td>Enter Address
:</td><td><input type=text name=a_add></td> </tr>
<tr> <td>Enter Subject
:</td><td><input type=text name=a_sub></td></tr>
<tr> <td><input type=submit
value=Show name=submit></td> </tr>
</table>
</center>
</form>
</body>
</html>
slip_18_insert.php
<?php
$a_no=$_GET['a_no'];
$a_name=$_GET['a_name'];
$a_add=$_GET['a_add'];
$a_sub=$_GET['a_sub'];
$con=@mysql_connect("localhost","root","");
$d=mysql_select_db("archana",$con);
$q2=mysql_query("insert into teachers
values($a_no,'$a_name','$a_add','$a_sub')");
echo "<center><b><h2>
Values Inserted Successfully.....!!!</h2><b></center>";
?>
slip_18_update.php
<?php
$con=@mysql_connect("localhost","root","");
if($_POST) {
$a_no=$_POST['a_no'];
$a_name=$_POST['a_name'];
$a_add=$_POST['a_add'];
$a_sub=$_POST['a_sub'];
$d=mysql_select_db("archana",$con);
$r=mysql_query("update teachers set
tid=$a_no,tname='$a_name',address='$a_add',subject='$a_sub' where
tid=$a_no");
header('Location: slip_18.html');
exit;
} else {
?>
<?php
//$c=mysql_connect("localhost","root","");
$d=mysql_select_db("archana",$con);
$q=mysql_query("select * from
teachers");
$row=mysql_fetch_array($q);
?>
<html>
<body>
<form action=""
method="post">
<center> <h2>Update Details
:</h2> <br>
<table>
<tr> <td> no
:</td><td><input type=text name=a_no value="<?php echo
$row[0];?>">
</td> </tr>
<tr> <td> name
:</td><td><input type=text name=a_name value="<?php echo
$row[1];?>"></td>
</tr>
<tr> <td> Address
:</td><td><input type=text name=a_add value="<?php echo
$row[2];?>"></td>
</tr>
<tr> <td> Subject
:</td><td><input type=text name=a_sub value="<?php echo
$row[3];?>"></td>
</tr>
<tr> <td><input type=submit
value=Show name=submit></td> </tr>
</table>
</center>
</form>
</body>
</html>
<?php } ?>