-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_customer.php
More file actions
153 lines (145 loc) · 4.31 KB
/
edit_customer.php
File metadata and controls
153 lines (145 loc) · 4.31 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
146
147
148
149
150
151
152
153
<?php
//Start the session to see if the user is authenticated user.
session_start();
//Check if the user is authenticated first. Or else redirect to login page
if(isset($_SESSION['IS_AUTHENTICATED']) && $_SESSION['IS_AUTHENTICATED'] == 1){
require('menu.php');
// Code to be executed when 'Insert' is clicked.
if ($_POST['submit'] == 'Insert'){
//validation flag to check that all validations are done
$validationFlag = true;
//Check all the required fields are filled
if(!($_POST['customer_id']))
{
echo '<center>Please fill valid details</center>';
$validationFlag = false;
}
else{
$customer_id = $_POST['customer_id'];
$customer_name = $_POST['customer_name'];
$phone_number = $_POST['phone_number'];
}
if($validationFlag){
//Connect to mysql server
$link = mysqli_connect('localhost', 'root', '');
//Check link to the mysql server
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,'test');
if(!$db){
die("Unable to select database");
}
//Create Insert query
$query = "INSERT INTO customer VALUES ('$customer_id', '$customer_name', '$phone_number')";
//Execute query
$results = mysqli_query($link,$query);
//Check if query executes successfully
if($results == FALSE)
echo mysqli_error($link) . '<br>';
else
echo '<center>Data inserted successfully! </center>';
}
}
// Code to be executed when 'Update' is clicked.
if ($_POST['submit'] == 'Update'){
if(!$_POST['customer_id'])
echo '<center>Enter the Id of the Customer</center>';
else{
$validationFlag = true;
$customer_id = $_POST['customer_id'];
$customer_name = $_POST['customer_name'];
$phone_number = $_POST['phone_number'];
if($_POST['customer_name'] && $_POST['phone_number'] ){
$update = "UPDATE customer SET customer_name = '$customer_name',phone_number = '$phone_number' WHERE customer_id = '$customer_id'";
}
else if($_POST['customer_name']){
$update = "UPDATE customer SET customer_name = '$customer_name' WHERE customer_id = '$customer_id'";
}
else if($_POST['phone_number']){
$update = "UPDATE customer SET phone_number = '$phone_number' WHERE customer_id = '$customer_id'";
}
//check if that id exist
$link = mysqli_connect('localhost', 'root', '');
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
$db = mysqli_select_db($link,'test');
if(!$db){
die("Unable to select database");
}
$sql_check = "SELECT * FROM customer WHERE customer_id='$customer_id'";
$result_check = mysqli_query($link,$sql_check);
$num=mysqli_num_rows($result_check);
if($num<=0){
die("<center>ID is invalid</center>");
}
//If all validations are correct, connect to MySQL and execute the query
if($validationFlag){
//Connect to mysql server
$link = mysqli_connect('localhost', 'root', '');
//Check link to the mysql server
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,'test');
if(!$db){
die("Unable to select database");
}
//Execute query
$results = mysqli_query($link,$update);
if($results == FALSE)
echo mysqli_error($link) . '<br>';
else
echo '<center>Entry updated</center>';
}
}
}
// Code to be executed when 'Delete' is clicked.
if ($_POST['submit'] == 'Delete'){
if(!$_POST['customer_id'])
echo '<center>Enter the Id of the Customer</center>';
else{
$customer_id = $_POST['customer_id'];
$link = mysqli_connect('localhost', 'root', '');
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
$db = mysqli_select_db($link,'test');
if(!$db){
die("Unable to select database");
}
$sql_check = "SELECT * FROM customer WHERE customer_id='$customer_id'";
$result_check = mysqli_query($link,$sql_check);
$num=mysqli_num_rows($result_check);
if($num<=0){
die("<center>ID is invalid</center>");
}
$delete = "DELETE FROM customer WHERE customer_id = '$customer_id'";
//Connect to mysql server
$link = mysqli_connect('localhost', 'root', '');
//Check link to the mysql server
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,'test');
if(!$db){
die("Unable to select database");
}
//Execute query
$results = mysqli_query($link,$delete);
if($results == FALSE)
echo mysqli_error() . '<br>';
else
echo '<center>Data deleted successfully</center>';
}
}
}
else{
header('location:login_modified.php');
exit();
}
?>