-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
91 lines (73 loc) · 2.17 KB
/
edit.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
<?php
require_once './connectDB.php';
// $sql = SQL code for executing
$sql = "SELECT * FROM students WHERE id=" . $_GET['id'] . " LIMIT 1";
// Execute SQL query and if successful, $query=TRUE or $query=FALSE
$result = $conn->query($sql);
// Get data \w $row
$row = $result->fetch_assoc();
// Show error message
if ($result === FALSE) {
echo $conn->error;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student List App | Edit</title>
<link rel="stylesheet" href="./default.css">
</head>
<body>
<h1>Student List App</h1>
<h3>Edit Form for #<?= $row['id'] ?></h3>
<main>
<form method="POST">
<label for="name">Student Name</label>
<br>
<input type="text" id="name" name="name" required value="<?= $row['name'] ?>" />
<br>
<br>
<label for="roll">Student ID</label>
<br>
<input type="text" id="roll" name="roll" required value="<?= $row['roll'] ?>" />
<br>
<br>
<label for="amount">Amount</label>
<br>
<input type="text" id="amount" name="amount" required value="<?= $row['amount'] ?>" />
<br>
<br>
<div style="text-align:center;">
<button class="btn btn-blue" type="submit" name="submit">Update</button>
<a class="btn btn-red" href="./index.php">Cancel</a>
</div>
</form>
</main>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
// $sql = SQL code for executing
$sql = "UPDATE students
SET
name='" . $_POST['name'] . "',
roll='" . $_POST['roll'] . "',
amount='" . $_POST['amount'] . "'
WHERE id=" . $row['id'] . "";
// Execute SQL query and if successful, $query=TRUE or $query=FALSE
$query = $conn->query($sql);
if ($query == TRUE) {
// *** Bellow code (commented) should be removed ***
// Show executable sql code
// echo $sql;
// Go to homepage
header('Location: index.php');
exit();
} else {
echo "Error editing record: " . $conn->error;
}
}
?>