-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpCRUD.php
145 lines (129 loc) · 3.13 KB
/
phpCRUD.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
$con=mysqli_connect("localhost","root","","test");
if($con)
{
if(isset($_POST['add']))
{
$name=trim($_POST['name']);
$marks=trim($_POST['marks']);
$count=mysqli_query($con,"select count(*) count from student");
$id=mysqli_fetch_assoc($count)['count']+1;
$sql="insert into student values(".$id.",'".$name."',".$marks.")";
if(mysqli_query($con,$sql))
{
echo "Added";
header("refresh:2;url=index.php");
}
else{
echo "Failed to Add";
header("refresh:2;url=index.php");
}
}
if(isset($_POST['update']))
{
$id=trim($_POST['id']);
$name=trim($_POST['name']);
$marks=trim($_POST['marks']);
$sql="update student set name='".$name."',marks=".$marks." where id=".$id;
if(mysqli_query($con,$sql))
{
echo "Updated";
header("refresh:2;url=index.php");
}
else{
echo "Failed Update";
header("refresh:2;url=index.php");
}
}
if(isset($_GET['did']))
{
$id=trim($_GET['did']);
$sql="delete from student where id=".$id;
if(mysqli_query($con,$sql))
{
echo "Deleted";
header("refresh:2;url=index.php");
}
else{
echo "Failed to Deleted";
header("refresh:2;url=index.php");
}
}
}
else
echo "NO Connection";
?>
<html>
<head>
<title>CRUD OPERATION</title>
</head>
<body>
<table border="1">
<tr>
<td>SNO</td>
<td>Name</td>
<td>Marks</td>
<td>Action</td>
</tr>
<?php
$con=mysqli_connect("localhost","root","","test");
if($con)
{
$res=mysqli_query($con,"select * from student");
if(mysqli_num_rows($res)>0)
{
while($row=mysqli_fetch_assoc($res))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['marks'];?></td>
<td><a href="index.php?uid=<?php echo $row['id'];?>">Edit</a>
<a href="index.php?did=<?php echo $row['id'];?>">Delete</a>
</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="4">No Data Found</td>
</tr>
<?php
}
}
?>
</table>
<?php
if(isset($_GET['uid']))
{
$id = trim($_GET['uid']);
$sql = "select * from student where id=".$id;
if($res = mysqli_query($con,$sql))
{
$row = mysqli_fetch_assoc($res);
$name = $row['name'];
$marks = $row['marks'];
?>
<form action="index.php" method="POST">
<input type="text" name="id" value="<?php echo $id?>" hidden="">
Name: <input type="text" placeholder="Enter Name" name="name" value="<?php echo $name;?>"><br>
Marks: <input type="number" placeholder="Enter Marks" name="marks" value="<?php echo $marks;?>"><br>
<input type="submit" name="update" value="Update">
<?php
}
}
else{
?>
<form action="index.php" method="POST">
Name: <input type="text" placeholder="Enter Name" name="name"><br>
Marks: <input type="number" placeholder="Enter Marks" name="marks"><br>
<input type="submit" name="add" value="Add">
<?php
}
?>
</body>
</html>