PHP program to store,display,update,and delete students details using Mysql database

PROGRAM

STUDENT.PHP

<html>
<body>
<form method="POST" action="">
<h2>Ente student detail</h2>
<PRE>
NAME :<input type='text' name='name' required>
LAST_NAME :<input type='text' name='last_name' required>
DEPARTMENT :<input type='text' name='department' required>
SUBJECT :<input type='text' name='subject' required>
PHONE :<input type='text' name='phone' required>
<input type='submit' value='submit' name='submit'>
</PRE>
</form>
</body>
</html>
<?php
$connection=mysqli_connect('localhost','root','','experiment');
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$lastname=$_POST['last_name'];
$department=$_POST['department'];
$subject=$_POST['subject'];
$phone=$_POST['phone'];
$query="insert into student(name,lastname,department,subject,phone)values('$name','$lastname','$department','$subject',$phone)";
$result=mysqli_query($connection,$query);
if(!$result)
{
echo "<script>alert('fasing problem with data')</script>";
exit;
}
else
{
echo "<script>alert('SUCCESSFUL')</script>";
}
}
/*SELECT ALL FROM TABLE*/

$query = "select * from student";
$result = mysqli_query($connection,$query);
if($result)
{
echo "<table border=1px;>";
echo "<tr><th>s_di</th>
<th>name</th>
<th>lastname</th>
<th>department</th>
<th>subject</th>
<th>phone</th>
<th>edit</th>
<th>delete</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['s_id'];
echo "</td>";
echo "<td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo $row['lastname'];
echo "</td>";
echo "<td>";
echo $row['department'];
echo "</td>";
echo "<td>";
echo $row['subject'];
echo "</td>";
echo "<td>";
echo $row['phone'];
echo "</td>";
echo "<td>";
echo "<a href='update.php?id=".$row['s_id']."'>edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='delete.php?id=".$row['s_id']."'>delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>



UPDATE.PHP

<html>
<head>
<?php
$s_id =$_GET['id'];
?>
</head>
<body>
<?php
$connection=mysqli_connect('localhost','root','','experiment');
$query="select * from student where s_id=$s_id";
$result=mysqli_query($connection,$query);
while($row = mysqli_fetch_array($result))
{
?>
<form method="POST" action="">
<h2>Ente student detail for update</h2>
<PRE>
NAME :<input type='text' name='name' value="<?php echo $row['name']; ?>"; >
LAST_NAME :<input type='text' name='last_name' value="<?php echo $row['lastname'];?>";>
DEPARTMENT :<input type='text' name='department' value="<?php echo $row['department'];?>";>
SUBJECT :<input type='text' name='subject' value="<?php echo $row['subject'];?>";>
PHONE :<input type='text' name='phone' value="<?php echo $row['phone'];?>";>
<input type='submit' value='submit' name='submit'>
</PRE>
</form>
</body>
</html>
<?php
}
?>
<?php
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$lastname=$_POST['last_name'];
$department=$_POST['department'];
$subject=$_POST['subject'];
$phone=$_POST['phone'];

$query="update student set name='$name',lastname='$lastname',department='$department',subject='$subject',phone='$phone' where s_id=$s_id";
$result=mysqli_query($connection,$query);
if(!$result)
{
echo "<script>alert('fasing problem with data')</script>";
exit;
}
else
{
echo "<script>alert('SUCCESSFUL')</script>";
echo "<script>document.location.href='exp 31 student.php'</script>";
}
}

?>

DELETE.PHP

<?php
$connection=mysqli_connect('localhost','root','','experiment');
$s_id=$_GET['id'];
$query="delete from student where s_id = $s_id";
$result=mysqli_query($connection,$query);
if($result)
{
echo "<script>alert('successfully removed')</script>";
echo "<script>document.location.href='exp 31 student.php'</script>";
}
else
{
echo "<script> alert('Failed to remove from subjects') </script>";
echo "<script>document.location.href='exp 31 student.php'</script>";
}

?>



OUTPUT



No comments:

Post a Comment