Sunday, July 9, 2017

2. Create a XHTML form with Name, Address Line 1, Address Line 2, and Email text fields. On submitting, store the values in MySQL table. Retrieve and display the data based on Name.

>create database stud;
>use stud;
>create table info(name varchar(40), add1 varchar(40), add2 varchar(40),
email varchar(50));
>desc info;

lab2.html

<html>
<body>
<center>
<table width="50%" border="2"><tr>
<form action="http://localhost/insert.php">
<tr><th> ENTER AGE INFORMATION </th></tr><br/>
<td align="center"><br/>Name: <input type="text" name="name"><br
/><br/>
add1: <input type="text" name="add1"><br /><br/>
add2: <input type="text" name="add2"><br /><br/>
email: <input type="text" name="email"><br /><br/>
submit: <input type="submit"><br/><br/></td>
</form></tr>
<tr><form action="http://localhost/search.php">
<tr><th> ENTER NAME TO BE SEARCHED AND DISPLAY ITS DETAILS</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>

insert.php


<?php
$link=mysqli_connect("localhost","root","","stud");
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'];
$add1=$_GET['add1'];
$add2=$_GET['add2'];
$email=$_GET['email'];
echo "hello " .$name;
echo " INSERTED SUCCESSFULLY<br/><br/>";
$query="insert into info values('$name','$add1','$add2','$email')";
mysqli_query($link,$query) or die(mysql_error());
$res=mysqli_query($link,"select * from info");
echo "<table border='1' align=center width=60%>";
echo "<tr><td width=15% align=center>NAME</td><td width=15%
align=center>ADDRESS1</td><td width=15% align=center>ADDRESS2</td><td
width=15% align=center>EMAIL ID</td></tr>";
while($arr=mysqli_fetch_row($res))
echo
"<tr><td>$arr[0]</td><td>$arr[1]</td><td>$arr[2]</td><td>$arr[3]</td></tr>"
;
echo "</table>";
?>



search.php

<?php
$link=mysqli_connect("localhost","root","","stud");
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'];
$res=mysqli_query($link,"select * from info where name='$name'");
if(mysqli_num_rows($res)==0)
{
echo "$name doesn't exist";
}
else
{
echo "<table border='1' align=center width=60%>";
echo "<tr><td width=15% align=center>NAME</td><td width=15%
align=center>ADDRESS1</td><td width=15% align=center>ADDRESS2</td><td
width=15% align=center>EMAIL ID</td></tr>";
while($arr=mysqli_fetch_row($res))
echo
"<tr><td>$arr[0]</td><td>$arr[1]</td><td>$arr[2]</td><td>$arr[3]</td><
/tr>";
echo "</table>";
}
?>

No comments:

Post a Comment