Home / Archive / Main Page
Duplicate Snippet

Embed Snippet on Your Site

Main Page

Star system Main Page.

Code Preview
php
<?php
<?php
// Database connection
$servername = "localhost";
$username = "star-admin";
$password = "My14nt566";
$dbname = "star";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// Add book
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['addBook'])) {
    $title = $_POST['title'];
    $author = $_POST['author'];
    $year = $_POST['year'];
    $sql = "INSERT INTO books (title, author, year) VALUES ('$title', '$author', '$year')";
    
    if ($conn->query($sql) === TRUE) {
        echo "New book added successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
// Fetch books
$sql = "SELECT id, title, author, year FROM books";
$result = $conn->query($sql);
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
    <title>CIU Textbook Library</title>
</head>
<body>
    <h1>High Education Library</h1>
    <h2>Add a New Book</h2>
    <form method="post" action="index.php">
        <label for="title">Title:</label><br>
        <input type="text" id="title" name="title" required><br><br>
        <label for="author">Author:</label><br>
        <input type="text" id="author" name="author" required><br><br>
        <label for="year">Year:</label><br>
        <input type="number" id="year" name="year" required><br><br>
        <input type="submit" name="addBook" value="Add Book">
    </form>
    <h2>Books List</h2>
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Title</th>
            <th>Author</th>
            <th>Year</th>
        </tr>
        <?php if ($result->num_rows > 0): ?>
            <?php while($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row["id"]; ?></td>
                    <td><?php echo $row["title"]; ?></td>
                    <td><?php echo $row["author"]; ?></td>
                    <td><?php echo $row["year"]; ?></td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr>
                <td colspan="4">No books found</td>
            </tr>
        <?php endif; ?>
    </table>
</body>
</html>

Comments

Add a Comment