0% found this document useful (0 votes)
32 views6 pages

SimpleCrud PHP

Uploaded by

aamilmalek90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

SimpleCrud PHP

Uploaded by

aamilmalek90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Connection.

php

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

[Link]
<?php
// first include connection
include '[Link]' ;
?>
<!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="./[Link]">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>

[Link]
<?php
// first include connection
include './[Link]';
// include "./[Link]";
// include('./[Link]');
// include("./[Link]");
?>
<!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="[Link]?id=<?php echo
$rows['id']?>">Edit</a></td>
<td><a onclick="return confirm('Are you sure, you
want to delete?')" href="[Link]?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="./[Link]">Home</a>
</center>

</body>
</html>

[Link]
<?php
include './[Link]';
$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="./[Link]">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")
[Link]("[Link]
[Link]","_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>

[Link]
<?php
include './[Link]';
$id = $_GET['id'];
$query = "delete from student1 where id = '$id' ";
$data = mysqli_query($con, $query);
if($data){
?>
<script>
alert('Record Deleted');
[Link]("[Link]
_self");
</script>
<?php
}else{
?>
<script>
alert('Record Not Inserted');
</script>
<?php
}
?>

You might also like