-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.php
71 lines (55 loc) · 1.96 KB
/
process.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
<?php
session_start();
//establish connection
$mysqli = new mysqli('localhost', 'root', '', 'crud-2') or die(mysqli_error($mysqli));
// set variables for edit click
$id = 0;
$update = false;
$title = "";
$author = "";
//check if addBook button is clicked
if (isset($_POST['addBook'])) {
$title = $_POST['title'];
$author = $_POST['author'];
// insert data into database
$insert = "INSERT INTO data (title , author) VALUES ('$title', '$author')";
$mysqli->query($insert) or die($mysqli->error);
$_SESSION['message'] = "A list without books is like a body without a soul. <br> Your book has been added!";
$_SESSION['msg_type'] = "success";
// redirect to index
header("location: index.php");
}
//check if delete button is clicked with get because an url is passed
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$delete = "DELETE FROM data WHERE id=$id";
$mysqli->query($delete) or die($mysqli->error);
$_SESSION['message'] = "Your book has been deleted! Hopefully that was not a mistake!";
$_SESSION['msg_type'] = "danger";
// redirect to index
header("location: index.php");
}
// check if edit button is clicked
if (isset($_GET["edit"])){
$id = $_GET["edit"];
$update = true;
$result = $mysqli->query("SELECT * FROM data WHERE id=$id") or die($mysqli->error());
// check if record exists
if (count(array($result))==1){
$row = $result->fetch_array();
$title = $row["title"];
$author = $row["author"];
}
}
//check if update button is clicked
if (isset($_POST["update"])){
$id = $_POST["id"];
$title = $_POST['title'];
$author = $_POST['author'];
$updateQuery = "UPDATE data SET title = '$title', author='$author' WHERE id=$id";
$mysqli->query($updateQuery) or die($mysqli->error);
$_SESSION['message'] = "Hooray, your book entry has been updated!";
$_SESSION['msg_type'] = "warning";
// redirect to index
header("location: index.php");
}