SimpleCrud PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

// Connection.

php

<?php
// connection
$con = mysqli_connect("localhost","root","Thispc@123","myphp");
if($con){
echo "Connected...";
}else{
echo "Not Connect";
}
?>

index.php
<?php
// first include connection
include 'Connection.php' ;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert Page</title>
</head>
<body>
<center>
<h2>Insert Data</h2>
<form action="" method="post">
<table>
<tr>
<td>First Name : </td>
<td><input type="text" placeholder="enter firstname"
name="firstname" required/> </td>
</tr>
<tr>
<td>Last Name : </td>
<td><input type="text" placeholder="enter lastname"
name="lastname" required/> </td>
</tr>
<tr>
<td>Age : </td>
<td><input type="text" placeholder="enter age" name="age"
required/> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="insert" value="Insert" />
</td>
</tr>
<tr>
<td></td>
<td><a href="./view.php">Display</a> </td>
</tr>

</table>

</form>
</center>

<?php
if(isset($_POST['insert'])){
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$age = $_POST['age'];

$query = "insert into student1(firstname, lastname, age)


values('$fname','$lname','$age')";
$data = mysqli_query($con,$query);
if($data){
// echo "<script>alert()</script>";
echo "<script>alert('Record Inserted')</script>";
}else{
echo "<script>alert('Record Not Inserted')</script>";
}
}
?>
</body>
</html>

view.php
<?php
// first include connection
include './Connection.php';
// include "./Connection.php";
// include('./Connection.php');
// include("./Connection.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Page</title>
</head>
<body>
<center>
<h2>Display Data</h2>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<th colspan="2"> Actions</th>
</tr>
<?php
$query = "select * from student1";
// $query = "select * from student2";
$data = mysqli_query($con, $query);
$result = mysqli_num_rows($data);

if($result){
while($rows = mysqli_fetch_array($data)){
?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['firstname']?></td>
<td><?php echo $rows['lastname']?></td>
<td><?php echo $rows['age']?></td>
<td><a href="update.php?id=<?php echo
$rows['id']?>">Edit</a></td>
<td><a onclick="return confirm('Are you sure, you
want to delete?')" href="delete.php?id=<?php echo
$rows['id']?>">Delete</a></td>
</tr>
<?php
}
}else{

?>
<tr style="text-align: center;" >
<td colspan="4" >No Record Found</td>
</tr>
<?php
}
?>
</table>
<br>
<br>
<a href="./index.php">Home</a>
</center>

</body>
</html>

update.php
<?php
include './Connection.php';
$id = $_GET['id'];
$query = "select * from student1 where id = '$id' ";
$data = mysqli_query($con,$query);
$row = mysqli_fetch_array($data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Page</title>
</head>
<body>
<center>
<h2>Edit Data</h2>
<form action="" method="post">
<table>
<tr>
<td>First Name : </td>
<td><input type="text" value="<?php echo
$row['firstname']?>" placeholder="enter firstname" name="firstname" required/>
</td>
</tr>
<tr>
<td>Last Name : </td>
<td><input type="text" value="<?php echo
$row['lastname']?>" placeholder="enter lastname" name="lastname" required/>
</td>
</tr>
<tr>
<td>Age : </td>
<td><input type="text" value="<?php echo $row['age']?>"
placeholder="enter age" name="age" required/> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="update" value="Edit" />
</td>
</tr>
<tr>
<td></td>
<td><a href="./view.php">Back</a> </td>
</tr>
</table>
</form>
</center>

<?php
if(isset($_POST['update'])){
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$age = $_POST['age'];
$query = "update student1 set firstname = '$fname',
lastname='$lname', age = '$age' where id = '$id' ";
$data = mysqli_query($con, $query);

if($data){
// echo "<script>alert()</script>";
?>
<script>
alert("Record Updated")
window.open("http://localhost/@learning/simpleCrud/vie
w.php","_self")
</script>
<?php
// echo "<script>alert('Record Updated')</script>";
}else{
?>

<script>alert("Record Not Updated")</script>


<?php
echo "<script>alert('Record Not Updated')</script>";
}
}
?>
</body>
</html>

delete.php
<?php
include './Connection.php';
$id = $_GET['id'];
$query = "delete from student1 where id = '$id' ";
$data = mysqli_query($con, $query);
if($data){
?>
<script>
alert('Record Deleted');
window.open("http://localhost/@learning/simpleCrud/view.php","
_self");
</script>
<?php
}else{
?>
<script>
alert('Record Not Inserted');
</script>
<?php
}
?>

You might also like