
========================================
COOKING SITE WEB APPLICATION - FULL SETUP GUIDE (PHP 8.2 / 8.3)
========================================

STUDENT INSTRUCTIONS
=====================

TOOLS REQUIRED:
---------------
- Uniform Server (Apache, MySQL, PHP 8.2 or 8.3)
- Browser (e.g., Chrome, Firefox)

PROJECT FILES:
--------------
- db.php (Database connection)
- register.php (User registration)
- login.php (User login)
- dishes.php (View dish instructions)
- contact.php (Submit feedback/comments)
- index.html (Landing page)
- style.css (Basic styling)
- cooking_site.sql (Database setup script)

========================================
STEP 1: INSTALL UNIFORM SERVER
========================================
1. Download from: https://www.uniformserver.com
2. Extract the zip file.
3. Run `UniController.exe`.
4. Start Apache and MySQL services.

========================================
STEP 2: SETUP DATABASE IN UNIFORM SERVER
========================================
1. Click "MySQL Console" in UniController.
2. Run the following SQL:

--- SQL START ---

CREATE DATABASE cooking_site;
USE cooking_site;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE,
    password VARCHAR(255)
);

CREATE TABLE dishes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    steps TEXT
);

CREATE TABLE comments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    dish_id INT,
    comment TEXT,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (dish_id) REFERENCES dishes(id)
);

CREATE TABLE comment_log (
    log_id INT AUTO_INCREMENT PRIMARY KEY,
    comment_id INT,
    log_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_user ON users(username);

INSERT INTO dishes (name, steps) VALUES 
('Spaghetti', '1. Boil pasta\n2. Make sauce\n3. Mix & serve'),
('Omelette', '1. Beat eggs\n2. Fry in pan\n3. Flip and serve'),
('Salad', '1. Chop veggies\n2. Add dressing\n3. Toss and serve'),
('Fried Rice', '1. Cook rice\n2. Stir fry with veggies\n3. Serve hot'),
('Pancakes', '1. Mix batter\n2. Fry on pan\n3. Flip and serve');

--- SQL END ---

========================================
STEP 3: STORED PROCEDURE & TRIGGER
========================================

DELIMITER //

CREATE PROCEDURE add_comment(IN uid INT, IN did INT, IN cmt TEXT)
BEGIN
    INSERT INTO comments(user_id, dish_id, comment) VALUES (uid, did, cmt);
END //

DELIMITER ;

DELIMITER //

CREATE TRIGGER after_comment_insert
AFTER INSERT ON comments
FOR EACH ROW
BEGIN
    INSERT INTO comment_log(comment_id) VALUES (NEW.id);
END //

DELIMITER ;

========================================
STEP 4: FOLDER SETUP
========================================
1. Go to: C:\UniServerZ\www
2. Create a folder: cooking
3. Place the below files in it.

========================================
STEP 5: PHP & HTML FILES
========================================

---- db.php ----
<?php
$conn = new mysqli("localhost", "root", "", "cooking_site");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

---- register.php ----
<?php include("db.php"); ?>
<form method="POST">
  Username: <input type="text" name="username"><br>
  Password: <input type="password" name="password"><br>
  <input type="submit" value="Register">
</form>
<?php
if ($_POST) {
    $user = $_POST['username'];
    $pass = $_POST['password'];
    $sql = "INSERT INTO users (username, password) VALUES ('$user', '$pass')";
    if ($conn->query($sql)) {
        echo "User registered!";
    } else {
        echo "Error: " . $conn->error;
    }
}
?>

---- login.php ----
<?php include("db.php"); session_start(); ?>
<form method="POST">
  Username: <input type="text" name="username"><br>
  Password: <input type="password" name="password"><br>
  <input type="submit" value="Login">
</form>
<?php
if ($_POST) {
    $user = $_POST['username'];
    $pass = $_POST['password'];
    $result = $conn->query("SELECT * FROM users WHERE username='$user' AND password='$pass'");
    if ($result->num_rows > 0) {
        $_SESSION['user'] = $user;
        header("Location: dishes.php");
    } else {
        echo "Invalid login!";
    }
}
?>

---- dishes.php ----
<?php include("db.php"); session_start(); ?>
<h2>Welcome <?php echo $_SESSION['user']; ?></h2>
<h3>Choose a dish to view steps:</h3>
<?php
$result = $conn->query("SELECT * FROM dishes");
while ($row = $result->fetch_assoc()) {
    echo "<h4>" . $row['name'] . "</h4>";
    echo "<p>" . nl2br($row['steps']) . "</p>";
}
?>
<a href='contact.php'>Give Feedback</a>

---- contact.php ----
<?php include("db.php"); session_start(); ?>
<form method="POST">
  Dish ID: <input type="number" name="dish_id"><br>
  Comment: <textarea name="comment"></textarea><br>
  <input type="submit" value="Submit Comment">
</form>
<?php
if ($_POST) {
    $user = $_SESSION['user'];
    $dish = $_POST['dish_id'];
    $comment = $_POST['comment'];
    $uid_result = $conn->query("SELECT id FROM users WHERE username='$user'");
    $uid = $uid_result->fetch_assoc()['id'];
    $conn->query("CALL add_comment($uid, $dish, '$comment')");
    echo "Comment added!";
}
?>

---- index.html ----
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Cooking Site</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Welcome to the Cooking Site!</h1>
  <p>Select an option to get started:</p>
  <ul>
    <li><a href="register.php">Register</a></li>
    <li><a href="login.php">Login</a></li>
    <li><a href="dishes.php">View Dishes</a></li>
    <li><a href="contact.php">Give Feedback</a></li>
  </ul>
  <footer><p>&copy; 2025 Cooking Site</p></footer>
</body>
</html>

---- style.css ----
body {
  font-family: Arial;
  background: #f2f2f2;
  padding: 20px;
}
form {
  background: #fff;
  padding: 10px;
  border-radius: 5px;
}

========================================
STEP 6: RUN & TEST YOUR APPLICATION
========================================
1. Start Apache and MySQL using UniController.
2. Open your browser and go to:
   http://localhost/cooking/index.html

========================================
STEP 7: BACKUP YOUR PROJECT
========================================
- Export MySQL database:
  mysqldump -u root cooking_site > cooking_site.sql
- Compress all files (*.php, *.html, style.css, cooking_site.sql) into a zip file.

========================================
✅ YOU'RE DONE!
You now have a complete and working PHP web app with MySQL.
