Repository navigation
Expand file tree
/
Copy pathoperation.php
More file actions
145 lines (118 loc) · 3.19 KB
/
Copy pathoperation.php
File metadata and controls
145 lines (118 loc) · 3.19 KB
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
require_once("../PHP-Project/db.php");
require_once("../PHP-Project/component.php");
$con = CreateDb();
//create button click
if (isset($_POST["create"])){
createData();
}
//update button click
if (isset($_POST["update"])){
updateData();
}
if(isset($_POST['delete'])){
deleteRecord();
}
if(isset($_POST['deleteall'])){
deleteAll();
}
function createData(){
$bookname = textBoxValue("book_name");
$bookpublisher = textBoxValue("book_publisher");
$bookprice = textBoxValue("book_price");
if ($bookname && $bookpublisher && $bookprice){
$sql = "INSERT INTO books(book_name,book_publisher,book_price)
VALUES('$bookname','$bookpublisher','$bookprice')";
if (mysqli_query($GLOBALS['con'],$sql)){
TextNode("success","Successfully Done");
}
else{echo "error";}
}
else{
TextNode("error","Provide Data !");
}
}
function textBoxValue($value){
$textbox = mysqli_real_escape_string($GLOBALS['con'],trim($_POST[$value]));
if (empty($textbox)){
return false;
}
else{
return $textbox;
}
}
function TextNode($classname,$msg){
$element = "<h6 class= '$classname'>$msg</h6>";
echo $element;
}
function getData(){
$sql = "SELECT * FROM books";
$result = mysqli_query($GLOBALS['con'],$sql);
if (mysqli_num_rows($result)> 0){
return $result;
}
}
function updateData(){
$bookId = textBoxValue("book_id");
$bookName = textBoxValue("book_name");
$bookPublisher = textBoxValue("book_publisher");
$bookPrice = textBoxValue("book_price");
if($bookName && $bookPublisher && $bookPrice){
$sql = "UPDATE books SET book_name='$bookName',
book_publisher='$bookPublisher',
book_price='$bookPrice'
WHERE id= '$bookId' ";
if (mysqli_query($GLOBALS['con'],$sql)){
TextNode("success","Data Updated !");
}
else{
TextNode("error","Failed to update data !");
}
}
else{
TextNode("error","Select Data to be updated !");
}
}
function deleteRecord(){
$bookid=(int)textBoxValue("book_id");
$sql="DELETE FROM books WHERE id=$bookid";
if(mysqli_query($GLOBALS['con'],$sql)){
TextNode("success","Record Deleted Successfully...!");
}else{
TextNode("error","Unable to DELETE RECORD...!");
}
}
function deleteBtn(){
$result=getData();
$i=0;
if($result){
while($row=mysqli_fetch_assoc($result)){
$i++;
if($i>3){
buttonElement("btn-deleteall","btn btn-danger","<i class='fas fa-trash'></i>Delete All","deleteall","");
return;
}
}
}
}
function deleteAll(){
$sql="DROP TABLE books";
if(mysqli_query($GLOBALS['con'],$sql)){
TextNode("success","All records deleted successfully...!");
CreateDb();
}
else{
TextNode("error","Something went wrong, records cannot be deleted...!");
}
}
// set id to textBox
function setID(){
$getid=getData();
$id=0;
if($getid){
while($row=mysqli_fetch_assoc($getid)){
$id=$row['id'];
}
}
return ($id+1);
}