diff --git a/db/data.php b/db/data.php index d8b2b4b5948c735cdf00dfec6c214379f6d2b83e..2b8c7e4f4a634b064b2c17efcd8c5b352b7c3b4c 100644 --- a/db/data.php +++ b/db/data.php @@ -1,10 +1,13 @@ <?php - include 'connect.php'; +include 'connect.php'; - // Make sure $db is defined and not null - if (isset($db)) { - try { - $sqlEvent = <<<EOF +// Make sure $db is defined and not null +if (isset($db)) { + try { + $db->beginTransaction(); + + // Insert events and tickets as before + $sqlEvent = <<<EOF INSERT INTO events (event_name, event_stock, event_price, event_date, event_location, gambar, vid) VALUES ('Music Concert', 2, 20, '2023-09-30 10:00:00', 'jakarta', 'assets/images/1.jpg', 'assets/videos/video.mp4'), ('Art Exhibition', 2, 15, '2023-10-05 15:30:00', 'prancis', 'assets/images/2.png', 'assets/videos/video.mp4'), @@ -27,45 +30,49 @@ ('Exhibitor Pass', 5); EOF; - $sqlUser = <<<EOF - INSERT INTO users (user_name, username, user_email, user_hashedPass, isAdmin) VALUES - ('John Doe', 'john_doe', 'john@example.com', 'hashed_password_1', true), - ('Jane Smith', 'jane_smith', 'jane@example.com', 'hashed_password_2', false), - ('Admin User', 'admin_user', 'admin@example.com', 'hashed_password_3', true), - ('Alice Johnson', 'alice', 'alice@example.com', 'hashed_password_4', false), - ('Bob Williams', 'bob', 'bob@example.com', 'hashed_password_5', false); - EOF; + $db->exec($sqlEvent); + $db->exec($sqlTicket); - $sqlPembelian = <<<EOF - INSERT INTO pembelian (ticket_id, user_id, pembelian_created_time) VALUES - (1, 1, '2023-10-01 12:30:00'), - (3, 2, '2023-10-06 16:15:00'), - (2, 3, '2023-11-15 20:00:00'), - (5, 4, '2023-12-05 10:45:00'), - (8, 5, '2024-01-20 08:30:00'); - EOF; + // Dummy user data with hashed passwords + $userData = [ + ['John Doe', 'john_doe', 'john@example.com', password_hash('password_1', PASSWORD_DEFAULT), 1], + ['Jane Smith', 'jane_smith', 'jane@example.com', password_hash('password_2', PASSWORD_DEFAULT), 0], + ['Admin User', 'admin_user', 'admin@example.com', password_hash('password_3', PASSWORD_DEFAULT), 1], + ['Alice Johnson', 'alice', 'alice@example.com', password_hash('password_4', PASSWORD_DEFAULT), 0], + ['Bob Williams', 'bob', 'bob@example.com', password_hash('password_5', PASSWORD_DEFAULT), 0] + ]; - $db->beginTransaction(); + $sqlUser = "INSERT INTO users (user_name, username, user_email, user_hashedPass, isAdmin) VALUES (?, ?, ?, ?, ?)"; + $stmt = $db->prepare($sqlUser); - $db->exec($sqlEvent); - // echo "Successfully inserted dummy data to events table<br/>"; + foreach ($userData as $user) { + $stmt->execute($user); + } - $db->exec($sqlTicket); - // echo "Successfully inserted dummy data to tickets table<br/>"; + $stmt->closeCursor(); - // $db->exec($sqlUser); - // echo "Successfully inserted dummy data to users table<br/>"; + // // Insert pembelian data + // $sqlPembelian = <<<EOF + // INSERT INTO pembelian (ticket_id, user_id, pembelian_created_time) VALUES + // (1, 1, '2023-10-01 12:30:00'), + // (3, 2, '2023-10-06 16:15:00'), + // (2, 3, '2023-11-15 20:00:00'), + // (5, 4, '2023-12-05 10:45:00'), + // (8, 5, '2024-01-20 08:30:00'); + // EOF; - // $db->exec($sqlPembelian); - // echo "Successfully inserted dummy data to pembelian table<br/>"; + // $db->exec($sqlPembelian); + // Commit the transaction $db->commit(); + + echo "Successfully inserted dummy data.<br/>"; } catch (PDOException $e) { $db->rollBack(); - // echo "Error: " . $e->getMessage(); + echo "Error: " . $e->getMessage(); } } else { - // echo "Error: Database connection not established."; + echo "Error: Database connection not established."; } $db = null;