CRUD operating PHP

<?php
// Connect to database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "data";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// CREATE operation
if (isset($_POST["create"])) {
  $name = $_POST["name"];
  $email = $_POST["email"];
  $sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
  if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
  } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  }
}

// DELETE operation
if (isset($_POST["delete"])) {
  $id = $_POST["id"];
  $sql = "DELETE FROM users WHERE id='$id'";
  if (mysqli_query($conn, $sql)) {
    echo "Record deleted successfully";
  } else {
    echo "Error deleting record: " . mysqli_error($conn);
  }
}

// UPDATE operation
if (isset($_POST["update"])) {
  $id = $_POST["id"];
  $name = $_POST["name"];
  $email = $_POST["email"];
  $sql = "UPDATE users SET name='$name', email='$email' WHERE id='$id'";
  if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
  } else {
    echo "Error updating record: " . mysqli_error($conn);
  }
}

// Select data from table
$sql = "SELECT id, name, email FROM users";
$result = mysqli_query($conn, $sql);

// Display data in table
if (mysqli_num_rows($result) > 0) {
  echo "<table>";
  echo "<tr><th>ID</th><th>Name</th><th>Email</th><th></th><th>Delete</th></tr>";
  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><a href='update.php?id=" . $row["id"] . "'></a></td>";
    echo "<td><form method='post' action='" . htmlspecialchars($_SERVER["PHP_SELF"]) . "'><input type='hidden' name='id' value='" . $row["id"] . "'><input type='submit' name='delete' value='Delete'></form></td>";
    echo "</tr>";
  }
  echo "</table>";
} else {
  echo "No results";
}

// Close database connection
mysqli_close($conn);
?>

<!DOCTYPE html>
<html>
<head>
  <title>PHP CRUD Example</title>
</head>
<body>

<h2>Create Record</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  Name: <input type="text" name="name"><br>
  Email: <input type="email" name="email"><br>
  <input type="submit" name="create" value="Create">
</form>
<h2>Update Data</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  ID: <input type="number" name="id"><br>
  Name: <input type="text" name="name"><br>
  Email: <input type="email" name="email"><br>
  <input type="submit" name="update" value="Update">
</form>
</body>
</html>



//for database
Create Database name as data

CREATE TABLE users
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE
);