-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.php
More file actions
66 lines (56 loc) · 2.12 KB
/
setup_db.php
File metadata and controls
66 lines (56 loc) · 2.12 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
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection to MySQL server (without selecting DB yet)
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS dayflow";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . $conn->error . "<br>";
}
// Select the database
$conn->select_db("dayflow");
// sql to create table
// 1. Create Companies Table - REMOVED
// The user requested to remove the companies table.
// All users will be managed directly in the users table without company association for now.
// 2. Create Users Table
$sql = "CREATE TABLE IF NOT EXISTS users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
login_id VARCHAR(50) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
phone VARCHAR(20) NOT NULL,
password VARCHAR(255) NOT NULL,
role ENUM('admin', 'employee', 'pending') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-- Note: Add FOREIGN KEY manually if needed, skipping strict constraint for simple setup script flexibility
)";
if ($conn->query($sql) === TRUE) {
echo "Table users created successfully<br>";
} else {
echo "Error creating users table: " . $conn->error . "<br>";
}
// 3. Setup Default Data
$result = $conn->query("SELECT * FROM users WHERE login_id = 'ADMIN001'");
if ($result->num_rows == 0) {
// Create Admin directly
$password_hash = password_hash("admin123", PASSWORD_DEFAULT);
$sql = "INSERT INTO users (login_id, full_name, email, phone, password, role)
VALUES ('ADMIN001', 'System Admin', 'admin@dayflow.com', '1234567890', '$password_hash', 'admin')";
if ($conn->query($sql) === TRUE) {
echo "Default admin and company created successfully.<br>";
}
} else {
echo "Default data already exists.<br>";
}
$conn->close();
echo "<br>Setup complete! You can now <a href='Login.php'>Login</a>.";
?>