104 lines
3.7 KiB
PHP
104 lines
3.7 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
|
|
header("location: login.php");
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$name = trim($_POST['name']);
|
|
$age = trim($_POST['age']);
|
|
$breed = trim($_POST['breed']);
|
|
$photo_url = trim($_POST['photo_url']);
|
|
$user_id = $_SESSION['id'];
|
|
|
|
if (empty($photo_url)) {
|
|
$photo_url = 'https://picsum.photos/500/500?random=' . mt_rand();
|
|
}
|
|
|
|
if (empty($name) || empty($age) || empty($breed)) {
|
|
$error = 'Please fill out all name, age, and breed fields.';
|
|
} else {
|
|
try {
|
|
$conn = db();
|
|
$sql = "INSERT INTO pets (user_id, name, age, breed, photo_url) VALUES (:user_id, :name, :age, :breed, :photo_url)";
|
|
$stmt = $conn->prepare($sql);
|
|
|
|
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
|
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
|
|
$stmt->bindParam(':age', $age, PDO::PARAM_INT);
|
|
$stmt->bindParam(':breed', $breed, PDO::PARAM_STR);
|
|
$stmt->bindParam(':photo_url', $photo_url, PDO::PARAM_STR);
|
|
|
|
if ($stmt->execute()) {
|
|
$success = "Pet added successfully!";
|
|
} else {
|
|
$error = "Something went wrong. Please try again later.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "Error: " . $e->getMessage();
|
|
}
|
|
$conn = null;
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Add Pet - Pet Tinder</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="css/style.css">
|
|
</head>
|
|
<body>
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
|
<div class="container-fluid">
|
|
<a class="navbar-brand" href="index.php">Pet Tinder</a>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="logout.php">Logout</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
<div class="container">
|
|
<h1 class="text-center mt-5">Add a New Pet</h1>
|
|
<?php if(!empty($error)): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
<?php if(!empty($success)): ?>
|
|
<div class="alert alert-success"><?php echo $success; ?></div>
|
|
<?php endif; ?>
|
|
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="mt-4">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Pet's Name</label>
|
|
<input type="text" name="name" id="name" class="form-control">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="age" class="form-label">Age</label>
|
|
<input type="number" name="age" id="age" class="form-control">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="breed" class="form-label">Breed</label>
|
|
<input type="text" name="breed" id="breed" class="form-control">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="photo_url" class="form-label">Photo URL</label>
|
|
<input type="text" name="photo_url" id="photo_url" class="form-control">
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">Add Pet</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|