Repository navigation
Expand file tree
/
Copy pathprocess.php
More file actions
150 lines (128 loc) · 4.66 KB
/
Copy pathprocess.php
File metadata and controls
150 lines (128 loc) · 4.66 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
session_start(); // Start the session
// Check if the user is logged in
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
// User is logged in, no need to perform logout actions
} else {
// User is not logged in, redirect to the login page
header('Location: login.php');
exit;
}
include 'functions.php';
// Database connection parameters
$host = 'mydb';
$username = 'root';
$password = '6%b48fCbUNZPfnQ';
$database = 'computers';
// Create a connection to the database
$conn = mysqli_connect($host, $username, $password, $database);
// Check the connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Function to fetch all computers from the database
function getComputers($conn) {
$computers = array();
// Prepare the SELECT statement
$sql = "SELECT * FROM computer_services";
// Execute the SELECT statement
$result = mysqli_query($conn, $sql);
// Check if any records are found
if (mysqli_num_rows($result) > 0) {
// Loop through each row and add the computer details to the array
while ($row = mysqli_fetch_assoc($result)) {
$computers[] = $row;
}
}
return $computers;
}
// Check if the form is submitted
if (isset($_POST['submit'])) {
// Get the form data
$id = $_POST['id'];
$name = $_POST['name'];
$model = $_POST['model'];
$serialNumber = $_POST['serial_number'];
$specifications = $_POST['specifications'];
// $Status_name = $_POST['Status_name'];
//$repair_types_name = $_POST['repair_types_name'];
// Check the action requested
if ($_POST['submit'] === 'Save') {
// Check if it's a new computer or an existing computer being updated
if (empty($id)) {
// Create a new computer
$sql = "INSERT INTO computer_services (name, model, serial_number, specifications)
VALUES ('$name', '$model', '$serialNumber', '$specifications')";
if (mysqli_query($conn, $sql)) {
echo '<p style="color: green; font-size: 20px; font-weight: bold;">Computer Created Successfully.</p>';
} else {
echo "Error creating computer: " . mysqli_error($conn);
}
// Your existing code
// After displaying a message or at the end of the file
echo '<p></p>';
redirectButton();
} else {
// Update an existing computer
$sql = "UPDATE computer_services SET
name = '$name',
model = '$model',
serial_number = '$serialNumber',
specifications = '$specifications',
Status_name = '$Status_name',
repair_types_name = '$repair_types_name'
WHERE id = $id";
if (mysqli_query($conn, $sql)) {
echo "Computer updated successfully.";
} else {
echo "Error updating computer: " . mysqli_error($conn);
}
}
} elseif ($_POST['submit'] === 'Delete') {
// Delete a computer
$sql = "DELETE FROM computer_services WHERE id = $id";
if (mysqli_query($conn, $sql)) {
echo "Computer deleted successfully.";
} else {
echo "Error deleting computer: " . mysqli_error($conn);
}
}
}
// Function to validate login
function login($username, $password, $conn) {
// Escape user inputs to prevent SQL injection
$username = mysqli_real_escape_string($conn, $username);
$password = mysqli_real_escape_string($conn, $password);
// Query to check if the admin key and password match in the database
$query = "SELECT * FROM admins WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) === 1) {
// Authentication successful
return true;
} else {
// Authentication failed
return false;
}
}
// Check if the login form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
// Get the username and password from the form
$username = $_POST['username'];
$password = $_POST['password'];
// Validate the login credentials
if (login($username, $password, $conn)) {
// Authentication successful
echo 'Login successful!';
// Store the user's login status in the session
$_SESSION['loggedin'] = true;
// Redirect to the home page or any other page
// header('Location: i.php');
// exit;
} else {
// Authentication failed
echo 'Invalid username or password.';
}
}
// Close the database connection
mysqli_close($conn);
?>