-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate.php
More file actions
125 lines (91 loc) · 2.85 KB
/
update.php
File metadata and controls
125 lines (91 loc) · 2.85 KB
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
<?php
require_once('auth.php');
?>
<html>
<head>
<link rel="shortcut icon" href="./images/dblogo.ico" />
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="./css/design.css">
<title>Update Data</title>
</head>
<body>
<div class="logo"></div>
<div class="design-block">
<center>
<h1>Update Data</h1>
<form action="update.php" method="get">
<input type="text" value="" placeholder="ID" name="id">
<button>Search</button>
</form>
<?php
include 'config.php';
//declare variable. use 'isset' to determine if variable is set and not null. if null, not execute
if(isset($_GET["id"]))
{
$ids = (isset($_GET['id']) ? $_GET['id'] : null);
try
{
//connect to database
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//insert data into db
$stmt = $conn->prepare("SELECT * FROM STUDENTS WHERE STUD_ID=?");
//execute sql query
$stmt->execute(array($ids));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $result['STUD_ID'];
$name = $result['STUD_NAME'];
$dob = $result['STUD_DOB'];
$phone = $result['STUD_PHONE'];
$ic = $result['STUD_IC'];
$course = $result['STUD_COURSE'];
echo "
<form action='update.php?id={$ids}' method='post'>
<input disable type='text' placeholder='{$id}' name='id'>
<input type='text' value='{$name}' placeholder='Name' name='name'>
<input type='text' value='{$dob}' placeholder='Date of Birth (YYYY-MM-DD)' name='dob'>
<input type='text' value='{$phone}' placeholder='Phone Number' name='phone'>
<input type='text' value='{$ic}' placeholder='IC' name='ic'>
<input type='text' value='{$course}' placeholder='Course' name='course'>
<button name='update'>Update</button>
</form>
<form action='update.php?id={$ids}' method='post' enctype='multipart/form-data' >
Choose an image to upload : <input type='file' name='fileToUpload' id='fileToUpload'>
<button name='upimg'>Upload Image</button>
</form>
";
if(isset($_POST['update']))
{
$name = $_POST['name'];
$dob = $_POST['dob'];
$phone = $_POST['phone'];
$ic = $_POST['ic'];
$course =$_POST['course'];
$stmt = $conn->prepare("UPDATE STUDENTS SET STUD_NAME=?, STUD_DOB=?, STUD_PHONE=?, STUD_IC=?,STUD_COURSE=?");
$stmt->execute(array($name,$dob,$phone,$ic,$course));
echo "<script>alert('Data updated.')</script>";
}
if(isset($_POST['upimg']))
{
//use image upload script
include 'imgupdate.php';
}
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
//close conection
$conn = null;
echo "
<br><br>
<div class='back-but'>
<a href='search.php?id={$ids}'><button type='button'>Back</button></a><br>
</div>
";
}
?>
</center>
</div>
</body>
</html>