Sie sind auf Seite 1von 9

1.

Write a perl program to insert name and age information entered by the user into a
table created using MySQL and to display the current contents of this table.
Lab1a.html

<html>
<head>
</head>
<body>
<form action="lab1.pl" method="post">
Name : <input type="text" name="name" /><br/>
Age : <input type="text" name="age"/><br/>
<input type="submit" />
</form>
</body>
</html>
Lab1b.html
#!C:\xampp\perl\bin\perl
use CGI ':standard';
use DBI;
print header;
$dbh=DBI->connect("DBI:mysql:lab","root","") or die("failed in databse connect");;
$name=param("name");
$age=param("age");
$qh=$dbh->prepare("insert into stud values('$name','$age')");
$qh->execute();
$qh=$dbh->prepare("select * from stud");
$qh->execute();
print start_html();
print "<table border='1' <tr><th> Name </th><th> Age </th></tr>";
while(@res=$qh->fetchrow_array())
{
print "<tr><td>$res[0] </td><td> $res[1] </td></tr> ";
}
print "</table>";
$qh->finish();
$dbh->disconnect();
print end_html();

2.Create XHTML form with Name, address line1, address line2 and email text fields. On
submitting, store the values in MySQL table. Retrieve and display the data based on
name
Lab2a.html

<html>
<head></head>
<body>
<form action="lab2b.php" method="post">
Name : <input type="text" name="name" /><br/>
Add1 : <input type="text" name="add1" /><br/>
Add2 : <input type="text" name="add2" /><br/>
Email : <input type="text" name="email" /><br/>
<input type="submit" />
</form>
</body>
</html>
Lab2b.php
<html>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$conn = new mysqli("localhost","root","","practice");
$name=$_POST['name'];
$add1=$_POST['add1'];
$add2=$_POST['add2'];
$mail=$_POST['email'];
$sql="insert into lab2 values('$name','$add1','$add2','$mail')";
$conn->query($sql) or die("Failed to insert the values !!!");
echo "values are inserted successfully";
$conn->close();
}
?>
<form action="lab2c.php" method="post">
Searching Option !!!!! <br/>
name of the student : <input type="text" name="name"/><br/>
<input type="submit" />
</form>
</body>
</html>
Lab2c.php

<html>
<head>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$conn=new mysqli("localhost","root","","practice");
$name=$_POST["name"];
$sql="select * from lab2 where name='$name'";
$res=$conn->query($sql) or die("failed to execute the query");
if(mysqli_num_rows($res)>0)
{
echo "<h2> Result Found </h2>";
echo "<table border='1'><th> Name </th><th> Add1 </th>
<th> Add2 </th><th> Email </th>";

while($row=$res->fetch_array())
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "result not found :";
}
}
else
{
echo "method should be post";
}
?>
</body>
</html>

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.
Lab3a.xml
<?xml version="1.0"?>
<studentinfo>
<student>
<name>Balaji </name>
<usn>1rn13mca32 </usn>
<age>22 </age>
</student>
<student>
<name>yogehs </name>
<usn>1ai13mca35 </usn>
<age>22 </age>
</student>
<student>
<name>Ashok </name>
<usn> 1rn13mca10 </usn>
<age> 22 </age>
</student>
<student>
<name>Vishnu </name>
<usn>1dr13mca32 </usn>
<age>22 </age>
</student>
</studentinfo>

Lab3b.php
<html>
<head>
</head>
<body>Inserted data is : <br/>
<?php
$conn=new mysqli("localhost","root","","practice");
$xml = simplexml_load_file("lab3a.xml");

echo "<table border='1'>";


foreach($xml->student as $student)
{
echo "<tr>";
echo "<td> Usn : </td><td> $student->usn </td>";
echo "<td> Name : </td><td> $student->name </td>";
echo "<td> Age : </td><td> $student->age </td>";
echo "</tr>";
$sql="insert into mum values('$student->usn','$student->name','$student->age')";
$conn->query($sql) or die("faile to store the data ");
}
echo "</table>";
?>
Search function :
<form action="awp3c.php" method="post"><br/>
Enter the usn : <input type="text" required="required" name="usn"/><br/>
<input type="submit"/>
</form>
</body>
</html>

Lab3c.php
<html>
<head>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$conn=new mysqli("localhost","root","","practice");
$usn=$_POST["usn"];

$sql="select * from mum where usn='$usn'";


$res=$conn->query($sql) or die("failed to execute the query");
echo "usn is : $usn";
if(mysqli_num_rows($res)>0)
{
echo "<h2> Result Found </h2>";
echo "<table border='1'><th> usn </th><th> Name </th><th> Age </th>";
while($row=$res->fetch_array())
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "result not found :";
}
}
else
{
echo "method should be post";
}
?>
</body>
</html>

Das könnte Ihnen auch gefallen