-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
147 lines (121 loc) · 5.04 KB
/
Copy pathedit.php
File metadata and controls
147 lines (121 loc) · 5.04 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
<?php
session_start();
// if user is not logged in
if( !$_SESSION['loggedInUser'] ) {
// send them to the login page
header("Location: index.php");
}
// get ID sent by GET collection
$clientID = $_GET['id'];
// connect to database
include('includes/connection.php');
// include functions file
include('includes/functions.php');
// query the database with client ID
$query = "SELECT * FROM clients WHERE id='$clientID'";
$result = mysqli_query( $conn, $query );
if( mysqli_num_rows($result) > 0 ) {
// we have data
// set some variables
while( $row = mysqli_fetch_assoc($result) ) {
$clientName = $row['name'];
$clientEmail = $row['email'];
$clientPhone = $row['phone'];
$clientAddress = $row['address'];
$clientCompany = $row['company'];
$clientNotes = $row['notes'];
}
} else { // no results returned
$alertMessage = "<div class='alert alert-warning'>Nothing to see here. <a href='clients.php'>Head back</a></div>";
}
// if update button was submitted
if( isset($_POST['update'] ) ) {
// set variables
$clientName = validateFormData( $_POST["clientName"] );
$clientEmail = validateFormData( $_POST["clientEmail"] );
$clientPhone = validateFormData( $_POST["clientPhone"] );
$clientAddress = validateFormData( $_POST["clientAddress"] );
$clientCompany = validateFormData( $_POST["clientCompany"] );
$clientNotes = validateFormData( $_POST["clientNotes"] );
// new database query & result
$query = "UPDATE clients SET
name='$clientName',
email='$clientEmail',
phone='$clientPhone',
address='$clientAddress',
company='$clientCompany',
notes='$clientNotes'
WHERE id='$clientID'";
$result = mysqli_query( $conn, $query );
if( $result ) {
// redirect to client page with query string
header("Location: clients.php?alert=updatesuccess");
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
if( isset($_POST['delete'] ) ) {
$alertMessage = "
<div class='alert alert-danger'>
<p>Are you sure you want to delete this client? No take backs!</p><br>
<form action='". htmlspecialchars( $_SERVER["PHP_SELF"] ) ."?id=$clientID' method='post'>
<input type='submit' class='btn btn-danger btn=sm' name='confirm-delete' value='Yes, delete!'>
<a type='button' class='btn btn-default btn-sm' data-dismiss='alert'>Oops, no thanks!</a>
</form>
</div>";
}
// if confirm delete button was submitted
if( isset($_POST['confirm-delete'] ) ) {
// new database query & result
$query = "DELETE FROM clients WHERE id=$clientID";
$result = mysqli_query( $conn, $query );
if( $result ) {
// redirect to client page with query string
header("Location: clients.php?alert=deleted");
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
//close the mysql connection
mysqli_close($conn);
include('includes/header.php');
?>
<h1>Edit Client</h1>
<?php echo $alertMessage; ?>
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>?id=<?php echo $clientID; ?>" method="post" class="row">
<div class="form-group col-sm-6">
<label for="client-name">Name</label>
<input type="text" class="form-control input-lg" id="client-name" name="clientName" value="<?php echo $clientName; ?>">
</div>
<div class="form-group col-sm-6">
<label for="client-email">Email</label>
<input type="text" class="form-control input-lg" id="client-email" name="clientEmail" value="<?php echo $clientEmail; ?>">
</div>
<div class="form-group col-sm-6">
<label for="client-phone">Phone</label>
<input type="text" class="form-control input-lg" id="client-phone" name="clientPhone" value="<?php echo $clientPhone; ?>">
</div>
<div class="form-group col-sm-6">
<label for="client-address">Address</label>
<input type="text" class="form-control input-lg" id="client-address" name="clientAddress" value="<?php echo $clientAddress; ?>">
</div>
<div class="form-group col-sm-6">
<label for="client-company">Company</label>
<input type="text" class="form-control input-lg" id="client-company" name="clientCompany" value="<?php echo $clientCompany; ?>">
</div>
<div class="form-group col-sm-6">
<label for="client-notes">Notes</label>
<textarea type="text" class="form-control input-lg" id="client-notes" name="clientNotes"><?php echo $clientNotes; ?></textarea>
</div>
<div class="col-sm-12">
<hr>
<button type="submit" class="btn btn-lg btn-danger pull-left" name="delete">Delete</button>
<div class="pull-right">
<a href="clients.php" type="button" class="btn btn-lg btn-default">Cancel</a>
<button type="submit" class="btn btn-lg btn-success" name="update">Update</button>
</div>
</div>
</form>
<?php
include('includes/footer.php');
?>