Advertisement
fsoc131y

tw: cs

Apr 3rd, 2024
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.83 KB | Source Code | 0 0
  1. 1. config.php:
  2. <?php
  3.  
  4. /* Database Connection */
  5.  
  6. $sDbHost = 'localhost';
  7. $sDbName = 'test';
  8. $sDbUser = 'root';
  9. $sDbPwd = '';
  10.  
  11. $dbConn = mysqli_connect($sDbHost, $sDbUser, $sDbPwd) or
  12.     die('MySQL connect failed. ' . mysqli_error($dbConn));
  13. mysqli_select_db($dbConn, $sDbName) or die('Cannot select database. ' . mysqli_error($dbConn));
  14.  
  15. 2. insert.php:
  16. <?php
  17. function valid($id, $name, $address, $city, $error)
  18. {
  19. ?>
  20.  
  21.     <html>
  22.  
  23.     <head>
  24.         <title>Insert Records</title>
  25.     </head>
  26.  
  27.     <body>
  28.  
  29.         <?php
  30.         if ($error != '') {
  31.             echo $error;
  32.         }
  33.         ?>
  34.  
  35.         <form action="" method="post">
  36.             Id:<input type="text" name="id"> <br>
  37.             Name:<input type="text" name="name"> <br>
  38.             Address:<input type="text" name="address"> <br>
  39.             City:<input type="text" name="city"> <br>
  40.             <input type="submit" name="submit" value="Insert Records">
  41.         </form>
  42.     </body>
  43.  
  44.     </html>
  45. <?php
  46. }
  47.  
  48. include('config.php');
  49.  
  50. if (isset($_POST['submit'])) {
  51.     $id = mysqli_real_escape_string($dbConn, $_POST['id']);
  52.     $name = mysqli_real_escape_string($dbConn, $_POST['name']);
  53.     $address = mysqli_real_escape_string($dbConn, $_POST['address']);
  54.     $city = mysqli_real_escape_string($dbConn, $_POST['city']);
  55.  
  56.     if ($id == '' || $name == '' || $address == '' || $city == '') {
  57.  
  58.         $error = 'Please enter the details!';
  59.  
  60.         valid($id, $name, $address, $city, $error);
  61.     } else {
  62.  
  63.         mysqli_query($dbConn, "INSERT into employee values ('$id','$name', '$address', '$city')")
  64.             or die(mysqli_error($dbConn));
  65.  
  66.         header("Location: view.php");
  67.     }
  68. } else {
  69.     valid('', '', '', '', '');
  70. }
  71. ?>
  72.  
  73. 3. edit.php:
  74. <?php
  75. function valid($id, $name, $address, $city, $error)
  76. {
  77. ?>
  78.     <html>
  79.  
  80.     <head>
  81.         <title>Edit Records</title>
  82.     </head>
  83.  
  84.     <body>
  85.         <?php
  86.  
  87.         if ($error != '') {
  88.             echo $error;
  89.         }
  90.         ?>
  91.  
  92.         <form action="" method="post">
  93.             Id:<input type="text" name="id" value="<?php echo $id; ?>" /> <br>
  94.             Name:<input type="text" name="name" value="<?php echo $name; ?>" /> <br>
  95.             Address:<input type="text" name="address" value="<?php echo $address; ?>" /> <br>
  96.             City:<input type="text" name="city" value="<?php echo $city; ?>" /> <br>
  97.             <input type="submit" name="submit" value="Edit Records">
  98.  
  99.         </form>
  100.     </body>
  101.  
  102.     </html>
  103. <?php
  104. }
  105.  
  106. include('config.php');
  107.  
  108. if (isset($_POST['submit'])) {
  109.  
  110.     if (is_numeric($_POST['id'])) {
  111.  
  112.         $id = mysqli_real_escape_string($dbConn, $_POST['id']);
  113.         $name = mysqli_real_escape_string($dbConn, $_POST['name']);
  114.         $address = mysqli_real_escape_string($dbConn, $_POST['address']);
  115.         $city = mysqli_real_escape_string($dbConn, $_POST['city']);
  116.  
  117.  
  118.         if ($id == '' || $name == '' || $address == '' || $city == '') {
  119.             $error = 'ERROR: Please fill in all required fields!';
  120.             valid($id, $name, $address, $city, $error);
  121.         } else {
  122.  
  123.             mysqli_query($dbConn, "UPDATE employee SET name='$name', address='$address' ,city='$city' WHERE id='$id'") or die(mysqli_error($dbConn));
  124.  
  125.             header("Location: view.php");
  126.         }
  127.     } else {
  128.  
  129.         echo 'Error in id!';
  130.     }
  131. } else {
  132.     if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
  133.  
  134.         $id = $_GET['id'];
  135.         $result = mysqli_query($dbConn, "SELECT * FROM employee WHERE id='$id'") or die(mysqli_error($dbConn));
  136.         $row = mysqli_fetch_array($result);
  137.  
  138.         if ($row) {
  139.  
  140.             $name = $row['name'];
  141.             $address = $row['address'];
  142.             $city = $row['city'];
  143.  
  144.             valid($id, $name, $address, $city, '');
  145.         } else {
  146.             echo "No results!";
  147.         }
  148.     } else {
  149.         echo 'Error!';
  150.     }
  151. }
  152. ?>
  153.  
  154. 4. view.php:
  155. <html>
  156.  
  157. <head>
  158.     <title>View Records</title>
  159. </head>
  160.  
  161. <body>
  162.     <?php
  163.  
  164.     include('config.php');
  165.  
  166.     $result = mysqli_query($dbConn, "SELECT * FROM employee")
  167.         or die(mysqli_error($dbConn));
  168.  
  169.     echo "<table border='1' cellpadding='10'>";
  170.     echo "<tr>
  171. <th><font color='Red'>Id</font></th>
  172. <th><font color='Red'>Name</font></th>
  173. <th><font color='Red'>Address</font></th>
  174. <th><font color='Red'>City</font></th>
  175. <th><font color='Red'>Edit</font></th>
  176. <th><font color='Red'>Delete</font></th>
  177. </tr>";
  178.  
  179.     while ($row = mysqli_fetch_array($result)) {
  180.  
  181.         echo "<tr>";
  182.         echo '<td><b><font color="Orange">' . $row['id'] .
  183.             '</font></b></td>';
  184.         echo '<td><b><font color="Orange">' . $row['name'] .
  185.             '</font></b></td>';
  186.         echo '<td><b><font color="Orange">' . $row['address'] .
  187.             '</font></b></td>';
  188.         echo '<td><b><font color="Orange">' . $row['city'] .
  189.             '</font></b></td>';
  190.  
  191.         echo '<td><b><font color="Orange">
  192.     <a href="edit.php?id=' .
  193.             $row['id'] . '">Edit</a></font></b></td>';
  194.         echo '<td><b><font color="Orange">
  195.     <a href="delete.php?id=' . $row['id'] .
  196.             '">Delete</a></font></b></td>';
  197.         echo "</tr>";
  198.     }
  199.     echo "</table>";
  200.     ?>
  201.     <p><a href="insert1.php">Insert new record</a></p>
  202. </body>
  203.  
  204. </html>
  205.  
  206. 5. delete.php:
  207. <?php
  208. include('config.php');
  209.  
  210. if (isset($_GET['id']) && is_numeric($_GET['id'])) {
  211.     $id = $_GET['id'];
  212.  
  213.     $result = mysqli_query($dbConn, "DELETE FROM employee WHERE id=$id")
  214.         or die(mysqli_error($dbConn));
  215.  
  216.     header("Location: view.php");
  217. } else {
  218.     header("Location: view.php");
  219. }
  220.  
  221.  
  222. ---------------------------------------------------------------------------------------------------------------------
  223. GPT:
  224. <?php
  225. // Database connection
  226. $servername = "localhost";
  227. $username = "root";
  228. $password = "";
  229. $database = "student_event";
  230.  
  231. $conn = mysqli_connect($servername, $username, $password, $database);
  232.  
  233. // Check connection
  234. if (!$conn) {
  235.     die("Connection failed: " . mysqli_connect_error());
  236. }
  237.  
  238. // Create table for student participation
  239. $sql = "CREATE TABLE IF NOT EXISTS participants (
  240.    id INT AUTO_INCREMENT PRIMARY KEY,
  241.    name VARCHAR(100) NOT NULL,
  242.    email VARCHAR(100) NOT NULL,
  243.    event VARCHAR(100) NOT NULL,
  244.    status VARCHAR(20) DEFAULT 'Registered'
  245. )";
  246.  
  247. if (mysqli_query($conn, $sql)) {
  248.     echo "Table 'participants' created successfully<br>";
  249. } else {
  250.     echo "Error creating table: " . mysqli_error($conn) . "<br>";
  251. }
  252.  
  253. // Insert operation
  254. if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
  255.     $name = $_POST['name'];
  256.     $email = $_POST['email'];
  257.     $event = $_POST['event'];
  258.  
  259.     $sql = "INSERT INTO participants (name, email, event) VALUES ('$name', '$email', '$event')";
  260.  
  261.     if (mysqli_query($conn, $sql)) {
  262.         echo "Record inserted successfully<br>";
  263.     } else {
  264.         echo "Error inserting record: " . mysqli_error($conn) . "<br>";
  265.     }
  266. }
  267.  
  268. // Update operation
  269. if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update'])) {
  270.     $id = $_POST['id'];
  271.     $status = $_POST['status'];
  272.  
  273.     $sql = "UPDATE participants SET status='$status' WHERE id=$id";
  274.  
  275.     if (mysqli_query($conn, $sql)) {
  276.         echo "Record updated successfully<br>";
  277.     } else {
  278.         echo "Error updating record: " . mysqli_error($conn) . "<br>";
  279.     }
  280. }
  281.  
  282. // Delete operation
  283. if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete'])) {
  284.     $id = $_POST['id'];
  285.  
  286.     $sql = "DELETE FROM participants WHERE id=$id";
  287.  
  288.     if (mysqli_query($conn, $sql)) {
  289.         echo "Record deleted successfully<br>";
  290.     } else {
  291.         echo "Error deleting record: " . mysqli_error($conn) . "<br>";
  292.     }
  293. }
  294.  
  295. mysqli_close($conn);
  296. ?>
  297.  
  298. -------
  299. <!DOCTYPE html>
  300. <html lang="en">
  301. <head>
  302.     <meta charset="UTF-8">
  303.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  304.     <title>Event Participation Form</title>
  305. </head>
  306. <body>
  307.     <h2>Event Participation Form</h2>
  308.     <form action="submit.php" method="POST">
  309.         <label for="name">Name:</label><br>
  310.         <input type="text" id="name" name="name" required><br>
  311.         <label for="email">Email:</label><br>
  312.         <input type="email" id="email" name="email" required><br>
  313.         <label for="event">Event:</label><br>
  314.         <input type="text" id="event" name="event" required><br>
  315.         <button type="submit">Submit</button>
  316.     </form>
  317. </body>
  318. </html>
  319.  
  320. --------------------
  321. <?php
  322. $servername = "localhost";
  323. $username = "root";
  324. $password = "";
  325. $database = "student_event";
  326.  
  327. $conn = mysqli_connect($servername, $username, $password, $database);
  328.  
  329. if (!$conn) {
  330.     die("Connection failed: " . mysqli_connect_error());
  331. }
  332.  
  333. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  334.     $name = $_POST['name'];
  335.     $email = $_POST['email'];
  336.     $event = $_POST['event'];
  337.  
  338.     $sql = "INSERT INTO participants (name, email, event) VALUES ('$name', '$email', '$event')";
  339.  
  340.     if (mysqli_query($conn, $sql)) {
  341.         echo "Record inserted successfully<br>";
  342.     } else {
  343.         echo "Error inserting record: " . mysqli_error($conn) . "<br>";
  344.     }
  345. }
  346.  
  347. mysqli_close($conn);
  348. header("Location: index.html");
  349. exit();
  350. ?>
  351.  
  352. ----------------------
  353. <!DOCTYPE html>
  354. <html lang="en">
  355. <head>
  356.     <meta charset="UTF-8">
  357.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  358.     <title>Participant List</title>
  359.     <style>
  360.         table {
  361.             border-collapse: collapse;
  362.             width: 100%;
  363.         }
  364.         th, td {
  365.             border: 1px solid #dddddd;
  366.             text-align: left;
  367.             padding: 8px;
  368.         }
  369.         th {
  370.             background-color: #f2f2f2;
  371.         }
  372.     </style>
  373. </head>
  374. <body>
  375.     <h2>Participant List</h2>
  376.     <table>
  377.         <tr>
  378.             <th>ID</th>
  379.             <th>Name</th>
  380.             <th>Email</th>
  381.             <th>Event</th>
  382.         </tr>
  383.         <?php
  384.         $servername = "localhost";
  385.         $username = "root";
  386.         $password = "";
  387.         $database = "student_event";
  388.  
  389.         $conn = mysqli_connect($servername, $username, $password, $database);
  390.  
  391.         if (!$conn) {
  392.             die("Connection failed: " . mysqli_connect_error());
  393.         }
  394.  
  395.         $sql = "SELECT * FROM participants";
  396.         $result = mysqli_query($conn, $sql);
  397.  
  398.         if (mysqli_num_rows($result) > 0) {
  399.             while ($row = mysqli_fetch_assoc($result)) {
  400.                 echo "<tr>";
  401.                 echo "<td>".$row['id']."</td>";
  402.                 echo "<td>".$row['name']."</td>";
  403.                 echo "<td>".$row['email']."</td>";
  404.                 echo "<td>".$row['event']."</td>";
  405.                 echo "</tr>";
  406.             }
  407.         } else {
  408.             echo "<tr><td colspan='4'>No participants found</td></tr>";
  409.         }
  410.  
  411.         mysqli_close($conn);
  412.         ?>
  413.     </table>
  414. </body>
  415. </html>
  416.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement