prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if (!$user) { header('Location: login.php'); exit; }
$startup_id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare("SELECT * FROM startups WHERE id = ?");
$stmt->execute([$startup_id]);
$startup = $stmt->fetch();
if (!$startup) { header('Location: startups.php'); exit; }
$platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
$error = '';
$success = '';
// Check if user is following
$stmt = db()->prepare("SELECT id FROM startup_followers WHERE user_id = ? AND startup_id = ?");
$stmt->execute([$user_id, $startup_id]);
$isFollowing = $stmt->fetch();
// Actions: follow/unfollow, invest, post_update, finish_round, cancel_round
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'follow') {
$stmt = db()->prepare("INSERT IGNORE INTO startup_followers (user_id, startup_id) VALUES (?, ?)");
$stmt->execute([$user_id, $startup_id]);
if ($stmt->rowCount() > 0) {
$stmt = db()->prepare("UPDATE startups SET followers_count = followers_count + 1 WHERE id = ?");
$stmt->execute([$startup_id]);
}
$success = "You are now following " . $startup['name'] . "!";
} elseif ($_POST['action'] === 'unfollow') {
$stmt = db()->prepare("DELETE FROM startup_followers WHERE user_id = ? AND startup_id = ?");
$stmt->execute([$user_id, $startup_id]);
if ($stmt->rowCount() > 0) {
$stmt = db()->prepare("UPDATE startups SET followers_count = GREATEST(0, followers_count - 1) WHERE id = ?");
$stmt->execute([$startup_id]);
}
$success = "You have unfollowed " . $startup['name'] . ".";
} elseif ($_POST['action'] === 'invest' && $user['role'] === 'investor') {
$amount = (float)($_POST['amount'] ?? 0);
if ($amount > 0) {
$stmt = db()->prepare("SELECT * FROM funding_rounds WHERE startup_id = ? AND status = 'Active'");
$stmt->execute([$startup_id]);
$round = $stmt->fetch();
if ($round) {
$stmt = db()->prepare("INSERT INTO investments (investor_id, startup_id, funding_round_id, amount, status) VALUES (?, ?, ?, ?, 'approved')");
$stmt->execute([$user_id, $startup_id, $round['id'], $amount]);
$stmt = db()->prepare("UPDATE funding_rounds SET funding_raised = funding_raised + ? WHERE id = ?");
$stmt->execute([$amount, $round['id']]);
$stmt = db()->prepare("UPDATE startups SET funding_raised = funding_raised + ? WHERE id = ?");
$stmt->execute([$amount, $startup_id]);
// Create notification for founder
$notif = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
$notif->execute([$startup['founder_id'], $user['full_name'] . " just invested £" . number_format($amount) . " in " . $startup['name'] . "!"]);
$success = "Investment of £" . number_format($amount) . " successfully processed!";
// Refresh data
$stmt = db()->prepare("SELECT * FROM startups WHERE id = ?");
$stmt->execute([$startup_id]);
$startup = $stmt->fetch();
}
}
} elseif ($_POST['action'] === 'post_update' && $user['role'] === 'founder' && $startup['founder_id'] == $user_id) {
$title = $_POST['update_title'] ?? '';
$content = $_POST['update_content'] ?? '';
if ($title && $content) {
// FIX: Added founder_id to the query to avoid 500 error
$stmt = db()->prepare("INSERT INTO startup_updates (startup_id, founder_id, title, content) VALUES (?, ?, ?, ?)");
$stmt->execute([$startup_id, $user_id, $title, $content]);
// Notify followers
$stmt = db()->prepare("SELECT user_id FROM startup_followers WHERE startup_id = ?");
$stmt->execute([$startup_id]);
$followers = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($followers)) {
$notif = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
foreach ($followers as $f_id) {
$notif->execute([$f_id, "New update from " . $startup['name'] . ": " . $title]);
}
}
$success = "Update posted successfully!";
}
} elseif ($_POST['action'] === 'finish_round' && $user['role'] === 'founder' && $startup['founder_id'] == $user_id) {
$stmt = db()->prepare("UPDATE funding_rounds SET status = 'Closed' WHERE startup_id = ? AND status = 'Active'");
$stmt->execute([$startup_id]);
$success = "Funding round successfully closed!";
} elseif ($_POST['action'] === 'cancel_round' && $user['role'] === 'founder' && $startup['founder_id'] == $user_id) {
db()->beginTransaction();
try {
// Find active round
$stmt = db()->prepare("SELECT id FROM funding_rounds WHERE startup_id = ? AND status = 'Active'");
$stmt->execute([$startup_id]);
$round = $stmt->fetch();
if ($round) {
// Cancel round
$stmt = db()->prepare("UPDATE funding_rounds SET status = 'Cancelled' WHERE id = ?");
$stmt->execute([$round['id']]);
// Refund all investments in this round
$stmt = db()->prepare("UPDATE investments SET status = 'Refunded' WHERE funding_round_id = ?");
$stmt->execute([$round['id']]);
// Deduct from startup total raised
$stmt = db()->prepare("SELECT SUM(amount) as total FROM investments WHERE funding_round_id = ? AND status = 'Refunded'");
$stmt->execute([$round['id']]);
$totalRefunded = $stmt->fetch()['total'] ?? 0;
$stmt = db()->prepare("UPDATE startups SET funding_raised = GREATEST(0, funding_raised - ?) WHERE id = ?");
$stmt->execute([$totalRefunded, $startup_id]);
db()->commit();
$success = "Funding round cancelled and all investments marked for refund.";
// Refresh data
$stmt = db()->prepare("SELECT * FROM startups WHERE id = ?");
$stmt->execute([$startup_id]);
$startup = $stmt->fetch();
} else {
db()->rollBack();
$error = "No active round found to cancel.";
}
} catch (Exception $e) {
db()->rollBack();
$error = "Error: " . $e->getMessage();
}
}
}
$stmt = db()->prepare("SELECT * FROM funding_rounds WHERE startup_id = ? AND status = 'Active'");
$stmt->execute([$startup_id]);
$activeRound = $stmt->fetch();
// Permission check for Funding History: Founders see their own, Investors see all.
$canSeeHistory = ($user['role'] === 'investor' || ($user['role'] === 'founder' && $startup['founder_id'] == $user_id));
// Fetch Funding History
$fundingHistory = [];
if ($canSeeHistory) {
$stmt = db()->prepare("
SELECT i.*, u.full_name as investor_name, u.profile_photo as investor_photo
FROM investments i
JOIN users u ON i.investor_id = u.id
WHERE i.startup_id = ? AND i.status != 'rejected'
ORDER BY i.created_at DESC
");
$stmt->execute([$startup_id]);
$fundingHistory = $stmt->fetchAll();
}
?>
= htmlspecialchars($startup['name']) ?> — = htmlspecialchars($platformName) ?>
= htmlspecialchars($error) ?>
= htmlspecialchars($success) ?>
= substr($startup['name'], 0, 1) ?>
= htmlspecialchars($startup['name']) ?>
Founded = date('M Y', strtotime($startup['created_at'])) ?>
= ucfirst($startup['status']) ?>
About the Venture
= htmlspecialchars($startup['description']) ?>
Funding History
No investment history available yet.
= substr($inv['investor_name'], 0, 1) ?>
= htmlspecialchars($inv['investor_name']) ?>
= date('M d, Y', strtotime($inv['created_at'])) ?>
= $inv['status'] === 'Refunded' ? '-' : '' ?>£= number_format($inv['amount']) ?>
= $inv['status'] === 'Refunded' ? 'Refunded' : 'Investment' ?>
Public Updates
New Update
prepare("SELECT * FROM startup_updates WHERE startup_id = ? ORDER BY created_at DESC");
$stmt->execute([$startup_id]);
$updates = $stmt->fetchAll();
?>
No updates have been posted yet.
= htmlspecialchars($upd['title']) ?>
Posted on = date('M d, Y', strtotime($upd['created_at'])) ?>
= htmlspecialchars($upd['content']) ?>
Venture Financials
£= number_format($startup['funding_raised']) ?>
Total Raised All-Time
= count($fundingHistory) ?>
Investments
= $startup['followers_count'] ?? 0 ?>
Followers
Active Round
0) ? min(100, ($activeRound['funding_raised'] / $activeRound['funding_goal']) * 100) : 0; ?>
£= number_format($activeRound['funding_raised']) ?>
£= number_format($activeRound['funding_goal']) ?> target
Investment Amount (£)
Back this Venture
This is your active funding round. Share the link with potential investors to reach your goal.
Finish Round Early
Cancel & Refund All
Founder
prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$startup['founder_id']]);
$founder = $stmt->fetch();
?>
= substr($founder['full_name'], 0, 1) ?>
= htmlspecialchars($founder['full_name'] ?? 'Founder') ?>
= htmlspecialchars($founder['university'] ?? 'Founder') ?>
Send Message