Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. config.php:
- <?php
- /* Database Connection */
- $sDbHost = 'localhost';
- $sDbName = 'test';
- $sDbUser = 'root';
- $sDbPwd = '';
- $dbConn = mysqli_connect($sDbHost, $sDbUser, $sDbPwd) or
- die('MySQL connect failed. ' . mysqli_error($dbConn));
- mysqli_select_db($dbConn, $sDbName) or die('Cannot select database. ' . mysqli_error($dbConn));
- 2. insert.php:
- <?php
- function valid($id, $name, $address, $city, $error)
- {
- ?>
- <html>
- <head>
- <title>Insert Records</title>
- </head>
- <body>
- <?php
- if ($error != '') {
- echo $error;
- }
- ?>
- <form action="" method="post">
- Id:<input type="text" name="id"> <br>
- Name:<input type="text" name="name"> <br>
- Address:<input type="text" name="address"> <br>
- City:<input type="text" name="city"> <br>
- <input type="submit" name="submit" value="Insert Records">
- </form>
- </body>
- </html>
- <?php
- }
- include('config.php');
- if (isset($_POST['submit'])) {
- $id = mysqli_real_escape_string($dbConn, $_POST['id']);
- $name = mysqli_real_escape_string($dbConn, $_POST['name']);
- $address = mysqli_real_escape_string($dbConn, $_POST['address']);
- $city = mysqli_real_escape_string($dbConn, $_POST['city']);
- if ($id == '' || $name == '' || $address == '' || $city == '') {
- $error = 'Please enter the details!';
- valid($id, $name, $address, $city, $error);
- } else {
- mysqli_query($dbConn, "INSERT into employee values ('$id','$name', '$address', '$city')")
- or die(mysqli_error($dbConn));
- header("Location: view.php");
- }
- } else {
- valid('', '', '', '', '');
- }
- ?>
- 3. edit.php:
- <?php
- function valid($id, $name, $address, $city, $error)
- {
- ?>
- <html>
- <head>
- <title>Edit Records</title>
- </head>
- <body>
- <?php
- if ($error != '') {
- echo $error;
- }
- ?>
- <form action="" method="post">
- Id:<input type="text" name="id" value="<?php echo $id; ?>" /> <br>
- Name:<input type="text" name="name" value="<?php echo $name; ?>" /> <br>
- Address:<input type="text" name="address" value="<?php echo $address; ?>" /> <br>
- City:<input type="text" name="city" value="<?php echo $city; ?>" /> <br>
- <input type="submit" name="submit" value="Edit Records">
- </form>
- </body>
- </html>
- <?php
- }
- include('config.php');
- if (isset($_POST['submit'])) {
- if (is_numeric($_POST['id'])) {
- $id = mysqli_real_escape_string($dbConn, $_POST['id']);
- $name = mysqli_real_escape_string($dbConn, $_POST['name']);
- $address = mysqli_real_escape_string($dbConn, $_POST['address']);
- $city = mysqli_real_escape_string($dbConn, $_POST['city']);
- if ($id == '' || $name == '' || $address == '' || $city == '') {
- $error = 'ERROR: Please fill in all required fields!';
- valid($id, $name, $address, $city, $error);
- } else {
- mysqli_query($dbConn, "UPDATE employee SET name='$name', address='$address' ,city='$city' WHERE id='$id'") or die(mysqli_error($dbConn));
- header("Location: view.php");
- }
- } else {
- echo 'Error in id!';
- }
- } else {
- if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
- $id = $_GET['id'];
- $result = mysqli_query($dbConn, "SELECT * FROM employee WHERE id='$id'") or die(mysqli_error($dbConn));
- $row = mysqli_fetch_array($result);
- if ($row) {
- $name = $row['name'];
- $address = $row['address'];
- $city = $row['city'];
- valid($id, $name, $address, $city, '');
- } else {
- echo "No results!";
- }
- } else {
- echo 'Error!';
- }
- }
- ?>
- 4. view.php:
- <html>
- <head>
- <title>View Records</title>
- </head>
- <body>
- <?php
- include('config.php');
- $result = mysqli_query($dbConn, "SELECT * FROM employee")
- or die(mysqli_error($dbConn));
- echo "<table border='1' cellpadding='10'>";
- echo "<tr>
- <th><font color='Red'>Id</font></th>
- <th><font color='Red'>Name</font></th>
- <th><font color='Red'>Address</font></th>
- <th><font color='Red'>City</font></th>
- <th><font color='Red'>Edit</font></th>
- <th><font color='Red'>Delete</font></th>
- </tr>";
- while ($row = mysqli_fetch_array($result)) {
- echo "<tr>";
- echo '<td><b><font color="Orange">' . $row['id'] .
- '</font></b></td>';
- echo '<td><b><font color="Orange">' . $row['name'] .
- '</font></b></td>';
- echo '<td><b><font color="Orange">' . $row['address'] .
- '</font></b></td>';
- echo '<td><b><font color="Orange">' . $row['city'] .
- '</font></b></td>';
- echo '<td><b><font color="Orange">
- <a href="edit.php?id=' .
- $row['id'] . '">Edit</a></font></b></td>';
- echo '<td><b><font color="Orange">
- <a href="delete.php?id=' . $row['id'] .
- '">Delete</a></font></b></td>';
- echo "</tr>";
- }
- echo "</table>";
- ?>
- <p><a href="insert1.php">Insert new record</a></p>
- </body>
- </html>
- 5. delete.php:
- <?php
- include('config.php');
- if (isset($_GET['id']) && is_numeric($_GET['id'])) {
- $id = $_GET['id'];
- $result = mysqli_query($dbConn, "DELETE FROM employee WHERE id=$id")
- or die(mysqli_error($dbConn));
- header("Location: view.php");
- } else {
- header("Location: view.php");
- }
- ---------------------------------------------------------------------------------------------------------------------
- GPT:
- <?php
- // Database connection
- $servername = "localhost";
- $username = "root";
- $password = "";
- $database = "student_event";
- $conn = mysqli_connect($servername, $username, $password, $database);
- // Check connection
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- // Create table for student participation
- $sql = "CREATE TABLE IF NOT EXISTS participants (
- id INT AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(100) NOT NULL,
- event VARCHAR(100) NOT NULL,
- status VARCHAR(20) DEFAULT 'Registered'
- )";
- if (mysqli_query($conn, $sql)) {
- echo "Table 'participants' created successfully<br>";
- } else {
- echo "Error creating table: " . mysqli_error($conn) . "<br>";
- }
- // Insert operation
- if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
- $name = $_POST['name'];
- $email = $_POST['email'];
- $event = $_POST['event'];
- $sql = "INSERT INTO participants (name, email, event) VALUES ('$name', '$email', '$event')";
- if (mysqli_query($conn, $sql)) {
- echo "Record inserted successfully<br>";
- } else {
- echo "Error inserting record: " . mysqli_error($conn) . "<br>";
- }
- }
- // Update operation
- if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update'])) {
- $id = $_POST['id'];
- $status = $_POST['status'];
- $sql = "UPDATE participants SET status='$status' WHERE id=$id";
- if (mysqli_query($conn, $sql)) {
- echo "Record updated successfully<br>";
- } else {
- echo "Error updating record: " . mysqli_error($conn) . "<br>";
- }
- }
- // Delete operation
- if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete'])) {
- $id = $_POST['id'];
- $sql = "DELETE FROM participants WHERE id=$id";
- if (mysqli_query($conn, $sql)) {
- echo "Record deleted successfully<br>";
- } else {
- echo "Error deleting record: " . mysqli_error($conn) . "<br>";
- }
- }
- mysqli_close($conn);
- ?>
- -------
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Event Participation Form</title>
- </head>
- <body>
- <h2>Event Participation Form</h2>
- <form action="submit.php" method="POST">
- <label for="name">Name:</label><br>
- <input type="text" id="name" name="name" required><br>
- <label for="email">Email:</label><br>
- <input type="email" id="email" name="email" required><br>
- <label for="event">Event:</label><br>
- <input type="text" id="event" name="event" required><br>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- --------------------
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $database = "student_event";
- $conn = mysqli_connect($servername, $username, $password, $database);
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $name = $_POST['name'];
- $email = $_POST['email'];
- $event = $_POST['event'];
- $sql = "INSERT INTO participants (name, email, event) VALUES ('$name', '$email', '$event')";
- if (mysqli_query($conn, $sql)) {
- echo "Record inserted successfully<br>";
- } else {
- echo "Error inserting record: " . mysqli_error($conn) . "<br>";
- }
- }
- mysqli_close($conn);
- header("Location: index.html");
- exit();
- ?>
- ----------------------
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Participant List</title>
- <style>
- table {
- border-collapse: collapse;
- width: 100%;
- }
- th, td {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
- th {
- background-color: #f2f2f2;
- }
- </style>
- </head>
- <body>
- <h2>Participant List</h2>
- <table>
- <tr>
- <th>ID</th>
- <th>Name</th>
- <th>Email</th>
- <th>Event</th>
- </tr>
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $database = "student_event";
- $conn = mysqli_connect($servername, $username, $password, $database);
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- $sql = "SELECT * FROM participants";
- $result = mysqli_query($conn, $sql);
- if (mysqli_num_rows($result) > 0) {
- while ($row = mysqli_fetch_assoc($result)) {
- echo "<tr>";
- echo "<td>".$row['id']."</td>";
- echo "<td>".$row['name']."</td>";
- echo "<td>".$row['email']."</td>";
- echo "<td>".$row['event']."</td>";
- echo "</tr>";
- }
- } else {
- echo "<tr><td colspan='4'>No participants found</td></tr>";
- }
- mysqli_close($conn);
- ?>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement