-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedituserproc.php
95 lines (91 loc) · 2.32 KB
/
edituserproc.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
<?php
require_once("checkadmin.php");
require_once('connect.php');
if (!isset($_POST['username']) || !isset($_POST['password']))
{
die("<font color='red'>Inputs are not set!.</font>");
}
$db = db_connect();
$stmt = $db->stmt_init();
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($_POST['delete']))
{
$stmt1 = $db->stmt_init();
$stmt2 = $db->stmt_init();
$sql = "SELECT id FROM student WHERE course=?";
$sql1 = "DELETE FROM session WHERE student=?";
$sql2 = "DELETE FROM student WHERE id=?";
$id = $_POST['id'];
// First delete orphaned sessions and students
if ($stmt->prepare($sql) && $stmt1->prepare($sql1) && $stmt2->prepare($sql2))
{
$stmt->bind_param('i',$id);
$stmt1->bind_param('i',$student_id);
$stmt2->bind_param('i',$student_id);
$stmt->bind_result($student_id);
$stmt->execute();
$stmt->store_result();
while($stmt->fetch())
{
$stmt1->execute();
$stmt2->execute();
}
$stmt->free_result();
$stmt1->close();
$stmt2->close();
}
// now delete orphaned course
if ($stmt->prepare("DELETE FROM course WHERE id=?"))
{
$stmt->bind_param('i',$id);
$stmt->execute();
}
// finally delete user
if ($stmt->prepare("DELETE FROM user WHERE id=?")){
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->close();
}
$db->close();
die();
}
else if (isset($_POST['id'])){
$id = $_POST['id'];
if ($stmt->prepare("UPDATE user SET username=?,password=? WHERE id=?"))
{
$hash = md5($password);
$stmt->bind_param('ssi',$username,$hash,$id);
$stmt->execute();
$stmt->close();
}
$db->close();
die("<font color='green'>User updated successfully</font>");
}else
{
// First check if user already exists
if ($stmt->prepare("SELECT * FROM user where username=?"))
{
$stmt->bind_param('s',$username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0)
{
$stmt->free_result();
$stmt->close();
$db->close();
die("<font color='red'>Username already exists!</font>");
}
$stmt->free_result();
}
$hash = md5($password);
if ($stmt->prepare("INSERT INTO user (username,password) VALUES (?,?)"))
{
$stmt->bind_param('ss',$username,$hash);
$stmt->execute();
$stmt->close();
}
$db->close();
die("<font color='green'>User created successfully</font>");
}
?>