DWPD : Practical 22
Script for index.php file<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('cart',$con);
?>
<html>
<head>
<title>Manipulate data From Database</title>
</head>
<body>
<form method="post">
<table>
<tr>
<td>Name</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>Department</td>
<td>
<select name="dept">
<option value="Computer Engineering">Computer</option>
<option value="Civil Engineering">Civil</option>
<option value="Mechanical Engineering">Mechanical</option>
</select>
</td>
</tr>
<tr>
<td>Semester</td>
<td>
<select name="sem">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="male">Male<br>
<input type="radio" name="gender" value="female">Female
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="insert" value="Insert New Student">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['insert']))
{
$name=$_POST['name'];
$dept=$_POST['dept'];
$sem=$_POST['sem'];
$gender=$_POST['gender'];
$sql="INSERT INTO student(Name,Dept,Sem,Gender) VALUES('$name','$dept','$sem','$gender')";
$query=mysql_query($sql);
if($query)
{
echo "Inserted successfully";
}
}
?>
<hr>
<br>
<table border="1">
<tr>
<th>ID</th><th>NAME</th><th>DEPARTMENT</th><th>SEMESTER</th><th>GENDER</th>
</tr>
<?php
$sql="SELECT * FROM student";
$query=mysql_query($sql);
while($raw=mysql_fetch_assoc($query))
{
?>
<tr>
<td><?php echo $raw['id'] ?></td>
<td><?php echo $raw['Name'] ?></td>
<td><?php echo $raw['Dept'] ?></td>
<td><?php echo $raw['Sem'] ?></td>
<td><?php echo $raw['Gender'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql="SELECT * FROM student";
$query=mysql_query($sql);
?>
<br>
<hr>
<form method="post">
<select name="id">
<option>Select Student ID</option>
<?php
while($raw=mysql_fetch_assoc($query))
{?>
<option value="<?php echo $raw['id'] ?>"><?php echo $raw['id'] ?></option>
<?php
}
?>
</select>
<input type="submit" value="get student" name="sel-id">
<br>
<br>
<?php
if(isset($_POST['sel-id']))
{
$id=$_POST['id'];
$sql="SELECT * FROM student WHERE id='".$id."'";
$query=mysql_query($sql);
while($raw=mysql_fetch_array($query))
{?>
<form method="post">
name : <input type="text" name="uname" value="<?php echo $raw['Name']; ?>"><br><br>
Department : <input type="text" name="udept" value="<?php echo $raw['Dept']; ?>"><br><br>
Semester : <input type="text" name="usem" value="<?php echo $raw['Sem']; ?>"><br><br>
<input type="hidden" name="uid" value="<?php echo $raw['id']; ?>">
<input type="submit" name="update" value="Update">
<input type="submit" name="delete" value="Delete"><br><br>
</form>
<?php
}
}
if(isset($_POST['update']))
{
$sid=$_POST['uid'];
$nm=$_POST['uname'];
$dpt=$_POST['udept'];
$sm=$_POST['usem'];
$sql = "UPDATE student SET Name='$nm', Dept='$dpt', Sem='$sm' WHERE student.id='$sid' LIMIT 1";
$query=mysql_query($sql);
if($query)
{
echo "data updated";
header("location:index.php");
}
}
elseif(isset($_POST['delete']))
{
$sid=$_POST['uid'];
$sql="DELETE FROM student WHERE id='$sid'";
$delete=mysql_query($sql);
if($delete)
{
header("location:index.php");
}
}
?>
</body>
</html>
Output :
Comments
Post a Comment