Crud Assignment
Crud Assignment
XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to National Examinations Board)
Lab Reports
Submitted by:
Bikrant Lekhak
023NEB737
Grade 12 “G”
Project Structure
/phpcrudbik/
├── config.php (Database configuration file)
├── header.php (HTML Header and form)
├── actions.php (Handles Insert, Update, Delete operations)
├── index.php (Main file to display data and form)
Config.php
<?php
$server = "localhost";
$username = "root";
$password = "";
$dbname = "biksql";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connection successful";
}
?>
OUTPUT
1
Header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD ASSIGNMENT</title>
</head>
<body>
<form method="POST" action="actions.php">
<label>Id</label>
<input type="text" name="id" id="id1" required/><br><br>
<label>Name</label>
<input type="text" name="name" id="name" required/><br><br>
<label>Address</label>
<input type="text" name="address" id="address" required/><br><br>
Actions.php
<?php
include('config.php');
2
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']))
{
if ($_POST['action'] == 'insert') {
if (isset($_POST['id'], $_POST['name'], $_POST['address']) &&
!empty($_POST['id']) && !empty($_POST['name']) &&
!empty($_POST['address'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$address = $_POST['address'];
$sql = "INSERT INTO info (id, name, address) VALUES ('$id', '$name',
'$address')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Please fill in all fields.";
}
}
$conn->close();
header("Location: index.php"); // Redirect back to index.php after action
}
?>
3
OUTPUT
For insert
For display
For delete
Index.php
<?php
include('config.php');
include('header.php');
4
<th>Actions</th>
</tr>";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['address'] . "</td>
<td>
<form method='POST' action='actions.php'>
<input type='hidden' name='id' value='" . $row['id'] . "'>
<button type='submit' name='action' value='delete'>Delete</button>
</form>
</td>
</tr>";
}
} else {
echo "<tr><td colspan='4'>No records found.</td></tr>";
}
echo "</table>";
$conn->close();
?>
</body>
</html>