/
phonebook.php
109 lines (98 loc) · 3.01 KB
/
phonebook.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
<?php
// Connect to database
include('connect-db.php');
// Function to save new contact
/**
* @param <string> $name // Name of the contact
* @param <string> $phone // The phone number of the contact
*/
function saveContact($name,$phone){
$sql="INSERT INTO contacts (full_name, phone_number) VALUES ('".$name."','".$phone."');";
$result=mysql_query($sql)or die(mysql_error());
}
// Function to delete contact
/**
* @param <int> id //the contact ID in database we wish to delete
*/
function deleteContact($id){
$sql="DELETE FROM contacts where id=".$id;
$result=mysql_query($sql);
}
// Function to edit contact name
/**
* @param <string> $name // Name of the contact
* @param <int> id // The contact ID in database we wish to delete
*/
function editName($name,$id) {
$sql = "UPDATE contacts SET
full_name='".$name."' WHERE id='".$id."'";
$result=mysql_query($sql)or die(mysql_error());
}
// Function to edit contact phone number
/**
* @param <string> $phone // The phone number of the contact
* @param <int> id // The contact ID in database we wish to delete
*/
function editPhone($phone,$id) {
$sql = "UPDATE contacts SET
phone_number='".$phone."' WHERE id='".$id."'";
$result=mysql_query($sql)or die(mysql_error());
}
// Let's get all the contacts
function getContacts(){
// Execute the sql to get all the contacts in database
$sql="SELECT * FROM contacts";
$result=mysql_query($sql);
// Store the contacts in an array of objects
$contacts=array();
while($record=mysql_fetch_object($result)){
array_push($contacts,$record);
}
// Return the contacts
return $contacts;
}
// Let's handle the AJAX calls now
$action=$_POST['action'];
if ($action=="add") {
// Get the post variables for the new contact
$name=$_POST['name'];
$phone=$_POST['phone'];
// Save the new contact
saveContact($name,$phone);
$output['msg']=$name." has been saved.";
// Reload the contacts
$output['contacts']=getContacts();
echo json_encode($output);
} else if ($action=="delete") {
// Collect the ID we wish to delete
$id=$_POST['id'];
//delete contact with that ID
deleteContact($id);
$output['msg']="Contact deleted.";
// Reload the contacts
$output['contacts']=getContacts();
echo json_encode($output);
} else if ($action=="editName") {
// Collect the ID we wish to edit
$id=$_POST['pk'];
// Get the post variables for the edited name
$name=$_POST['value'];
// Update name in database
editName($name,$id);
$output['msg']="Contact name updated.";
echo json_encode($output);
} else if ($action=="editPhone") {
// Collect the ID we wish to edit
$id=$_POST['pk'];
// Get the post variables for the edited phone number
$phone=$_POST['value'];
// Update phone number in database
editPhone($phone,$id);
$output['msg']="Contact phone number updated.";
echo json_encode($output);
} else {
$output['contacts']=getContacts();
$output['msg']="List of all contacts";
echo json_encode($output);
}
?>