-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.php
107 lines (86 loc) · 3.67 KB
/
add.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
<?php
session_start();
// if user is not logged in
if( !$_SESSION['loggedInUser'] ) {
// send them to the login page
header("Location: index.php");
}
// connect to database
include('includes/connection.php');
// include functions file
include('includes/functions.php');
if( isset( $_POST['add'] ) ) {
// set all variables to empty by default
$clientName = $clientEmail = $clientPhone = $clientAddress = $clientCompany = $clientNotes = "";
// check to see if inputs are empty
// create variables with form data
// wrap the data with our function
if( !$_POST["clientName"] ) {
$nameError = "Please enter a name <br>";
} else {
$clientName = validateFormData( $_POST["clientName"] );
}
if( !$_POST["clientEmail"] ) {
$nameError = "Please enter an email <br>";
} else {
$clientEmail = validateFormData( $_POST["clientEmail"] );
}
// these inputs are not required
// so we'll just store whatever has been entered
$clientPhone = validateFormData( $_POST["clientPhone"] );
$clientAdress = validateFormData( $_POST["clientAddress"] );
$clientCompany = validateFormData( $_POST["clientCompany"] );
$clientNotes = validateFormData( $_POST["clientNotes"] );
// if required fields have data
if( $clientName && $clientEmail ) {
// create query
$query = "INSERT INTO clients (id, name, email, phone, address, company, notes, date_added) VALUES (NULL, '$clientName', '$clientEmail', '$clientPhone', '$clientAdress', '$clientCompany', '$clientNotes', CURRENT_TIMESTAMP)";
$result = mysqli_query( $conn, $query );
// if query was successful
if( $result ) {
// refresh page with query string
header( "Location: clients.php?alert=success" );
} else {
// something went wrong
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
}
}
// close myslq connection
mysqli_close($conn);
include('includes/header.php');
?>
<h1>Add Client</h1>
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>" 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="">
</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="">
</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="">
</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="">
</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="">
</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"></textarea>
</div>
<div class="col-sm-12">
<a href="clients.php" type="button" class="btn btn-lg btn-default">Cancel</a>
<button type="submit" class="btn btn-lg btn-success pull-right" name="add">Add Client</button>
</div>
</form>
<?php
include('includes/footer.php');
?>