-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
93 lines (65 loc) · 1.61 KB
/
db.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
<?php
session_start();
$conn = mysqli_connect("localhost","root","","curling");
if($conn->connect_error)
{
die("Error on conecting with DB");
}
function getList($table,$where)
{
global $conn;
$list = [];
$query = "SELECT * FROM $table";
if(!empty($where))
{
$query = "SELECT * FROM $table WHERE $where";
}
$result = $conn->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$list[]= $row;
}
}
return $list;
}
function updateData($id, $inset_arr,$table)
{
global $conn;
$query = "UPDATE $table SET $inset_arr WHERE id = $id";
$result = $conn->query($query);
header("Location:user.php?isTrue=1");
}
function insertData($table,$colums,$values)
{
global $conn;
$query = "INSERT INTO $table($colums)VALUES($values)";
$result = $conn->query($query);
}
function deleteData($table,$where)
{
global $conn;
$result = $conn->query("DELETE FROM $table WHERE $where");
}
function add_news_data($obj)
{
$colums = "title,description,date,pic,created_at,updated_at";
$title =$obj['title'];
$description = $obj['desc'];
$date = $obj['date'];
$pic = $obj['pic'];
$created_at = date("Y-m-d H:i:s");
$updated_at = null;
$values = "'$title','$description','$date','$pic','$created_at','$updated_at'";
$data_exist = getList("news","title='$title'");
if(!empty($data_exist))
{
$updated_at = date("Y-m-d H:i:s");
$updated_values = "title='$title',description='$description',date='$date',pic='$pic',updated_at='$updated_at'";
updateData($data_exist[0]["id"],$updated_values,"news");
}
else
{
insertData("news",$colums,$values);
}
}
?>