-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
140 lines (131 loc) · 4.09 KB
/
index.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
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
<?php
$title = "This is the title";
$artist = "Artist 1";
$price = "100";
$year = "2018-01-01";
/**
* Function for database connection using PDO class
**/
function dbCon() {
try {
// Server name according to your configuration. For most case host name will be localhost in local database.
$servername = "eu-cdbr-west-02.cleardb.net";
// Name of database that you have created.
$dbname = "heroku_b5ea113c443cc79";
// Username of database. For most case it will be root in local database.
$username = "b057b186fe4e26";
// Password of the database. For most case it will be empty in local database
$password = "02e2fafa";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
return $conn;
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
/**
* Insert operation to MySql database using prepared statement of PDO class
* insert() function takes $title (string), $artist (string), $price (string) and $year(datetime (1999-01-0)) as parameter.
**/
function insert($title,$artist,$price,$year) {
try {
$conn = dbCon();
$sql = $conn->prepare("INSERT INTO cds (title, artist, price, year) VALUES (:title,:artist,:price,:year)");
$sql->bindParam(':title', $title);
$sql->bindParam(':artist', $artist);
$sql->bindParam(':price', $price);
$sql->bindParam(':year', $year);
$insertRow= $sql->execute();
echo "$insertRow row inserted";
} catch(PDOException $e) {
echo "Something went wrong";
}
}
/**
* Function to delete the all records from database.
**/
function delete() {
$conn = dbCon();
$sql = $conn->prepare("DELETE FROM cds");
$sql->execute();
echo "Deleted successfully";
}
/**
* Function to update the records.
* Values need to be updated is required to be supplied as parameter. If value is not change then old value need to be passed else it will store null
* in database
**/
function update($title,$artist,$price,$year) {
$conn = dbCon();
$sql = $conn->prepare("UPDATE cds SET title=:title, artist=:artist, price=:price, year=:year");
$sql->bindParam(':title', $title);
$sql->bindParam(':artist', $artist);
$sql->bindParam(':price', $price);
$sql->bindParam(':year', $year);
echo "Updated successfully";
}
/**
* Function to get all the records form database.
* fetchAll() function will fetch all the records from database as an associative array and fetch() will fetch only one record from database.
* {variableName} is an string interpolation in php. It provides a way to embed a variable, an array value,
* or an object property in a string with a minimum of effort.
**/
function getCds() {
$conn = dbCon();
$sql = $conn->prepare("SELECT * FROM cds");
$sql->execute();
$results = $sql->fetchAll();
foreach($results as $result) {
echo "<tr>";
echo "<td>{$result["id"]}</td>";
echo "<td>{$result["title"]}</td>";
echo "<td>{$result["artist"]}</td>";
echo "<td>{$result["price"]}</td>";
echo "<td>{$result["year"]}</td>";
echo "</tr>";
}
}
/**
* Function to delete record by id
**/
function deletById($id) {
$conn = dbCon();
$sql = $conn->prepare("DELETE FROM cds WHERE id=:id");
$sql->bindParam(':id', $id);
$sql->execute();
echo "Deleted successfully";
}
//insert($title,$artist,$price,$year);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>
ID
</th>
<th>
Title
</th>
<th>
Artist
</th>
<th>
Price
</th>
<th>
Year
</th>
</tr>
</thead>
<tbody>
<?php getCds(); ?>
</tbody>
</table>
</body>
</html>