-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
266 lines (224 loc) · 9.32 KB
/
db.php
File metadata and controls
266 lines (224 loc) · 9.32 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
// Start output buffering to prevent "headers already sent" errors
ob_start();
// Database configuration
$host = 'localhost';
$username = 'root';
$password = ''; // Empty password for XAMPP default setup
$database = 'shop';
// Base URL
define('BASE_URL', 'http://localhost/maoni');
// Error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
function connectDB() {
global $host, $username, $password, $database;
static $conn = null;
if ($conn === null) {
try {
// First, check if we can connect to MySQL
$conn = new mysqli($host, $username, $password);
if ($conn->connect_error) {
throw new Exception("Failed to connect to MySQL: " . $conn->connect_error);
}
// Check if database exists, create it if it doesn't
if (!databaseExists($conn, $database)) {
$conn->query("CREATE DATABASE IF NOT EXISTS $database");
// Select the newly created database
$conn->select_db($database);
// Create all necessary tables
initializeDatabase($conn);
} else {
// Select the existing database
$conn->select_db($database);
}
// Set charset to prevent SQL injection
$conn->set_charset("utf8");
} catch (Exception $e) {
// Output a user-friendly message and log the error
echo "<div style='background-color: #ffdddd; color: #990000; padding: 15px; margin: 10px 0; border-radius: 5px;'>
<h3>Database Connection Error</h3>
<p>Unable to connect to the database. Please check your configuration or contact the administrator.</p>
<p><strong>Error details (for admin only):</strong> " . $e->getMessage() . "</p>
<p><strong>Troubleshooting:</strong> If you're seeing 'Access denied', check that your MySQL server is running and that your credentials are correct.</p>
</div>";
exit;
}
}
return $conn;
}
// Helper function to check if database exists
function databaseExists($conn, $database) {
$result = $conn->query("SHOW DATABASES LIKE '$database'");
return ($result->num_rows > 0);
}
// Helper function for safe SQL queries with prepared statements
function executeQuery($sql, $params = [], $types = "") {
$conn = connectDB();
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Error in preparing statement: " . $conn->error);
}
if (!empty($params)) {
if (empty($types)) {
// Auto-determine types if not provided
$types = str_repeat("s", count($params));
}
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
return $stmt;
}
// Get results as an associative array from a SELECT query
function fetchResults($sql, $params = [], $types = "") {
$stmt = executeQuery($sql, $params, $types);
$result = $stmt->get_result();
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
$stmt->close();
return $data;
}
// Get a single row from a SELECT query
function fetchRow($sql, $params = [], $types = "") {
$data = fetchResults($sql, $params, $types);
return !empty($data) ? $data[0] : null;
}
// Insert, update, or delete records
function modifyData($sql, $params = [], $types = "") {
$stmt = executeQuery($sql, $params, $types);
$affected = $stmt->affected_rows;
$stmt->close();
return $affected;
}
// Initialize database and tables if they don't exist
function initializeDatabase($conn = null) {
if ($conn === null) {
$conn = connectDB();
}
try {
// Check if users table exists
$result = $conn->query("SHOW TABLES LIKE 'users'");
if ($result->num_rows === 0) {
// Create users table
$sql = "CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
phone VARCHAR(15),
address TEXT,
password VARCHAR(255) NOT NULL,
is_admin TINYINT(1) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_username (username),
INDEX idx_email (email)
)";
if (!$conn->query($sql)) {
throw new Exception("Error creating users table: " . $conn->error);
}
// Insert admin user
$adminPassword = password_hash('admin123', PASSWORD_DEFAULT);
$sql = "INSERT INTO users (first_name, last_name, username, email, phone, address, password, is_admin)
VALUES ('Admin', 'User', 'admin', 'admin@shopway.com', '1234567890', '123 Admin St', ?, 1)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $adminPassword);
$stmt->execute();
$stmt->close();
// Create other essential tables
createTable($conn, 'categories');
createTable($conn, 'products');
createTable($conn, 'orders');
createTable($conn, 'order_items');
createTable($conn, 'sessions');
echo "<div style='background-color: #d4edda; color: #155724; padding: 15px; margin: 10px 0; border-radius: 5px;'>
<h3>Database Initialized</h3>
<p>Database and tables created successfully.</p>
<p>Default admin credentials: username: admin, password: admin123</p>
</div>";
}
return true;
} catch (Exception $e) {
echo "<div style='background-color: #ffdddd; color: #990000; padding: 15px; margin: 10px 0; border-radius: 5px;'>
<h3>Database Initialization Error</h3>
<p>" . $e->getMessage() . "</p>
</div>";
return false;
}
}
// Create a specific table if it doesn't exist
function createTable($conn, $tableName) {
switch ($tableName) {
case 'categories':
$sql = "CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_category_name (name)
)";
break;
case 'products':
$sql = "CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10,2) NOT NULL,
stock INT DEFAULT 0,
image VARCHAR(255) DEFAULT 'default.jpg',
category_id INT,
featured TINYINT(1) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL
)";
break;
case 'orders':
$sql = "CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
status ENUM('Pending', 'Processing', 'Shipped', 'Delivered', 'Cancelled') DEFAULT 'Pending',
payment_method VARCHAR(50),
shipping_address TEXT NOT NULL,
contact_phone VARCHAR(15) NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)";
break;
case 'order_items':
$sql = "CREATE TABLE order_items (
id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
price DECIMAL(10,2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
)";
break;
case 'sessions':
$sql = "CREATE TABLE sessions (
id VARCHAR(128) NOT NULL PRIMARY KEY,
user_id INT,
ip_address VARCHAR(45) NOT NULL,
user_agent TEXT,
payload TEXT NOT NULL,
last_activity INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)";
break;
default:
return false;
}
$conn->query($sql);
return true;
}
// Initialize database at startup
initializeDatabase();
?>