0% found this document useful (0 votes)
72 views7 pages

Crud Assignment

Uploaded by

lekhakbikrant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views7 pages

Crud Assignment

Uploaded by

lekhakbikrant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ST.

XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to National Examinations Board)

Lab Reports

Submitted by:
Bikrant Lekhak
023NEB737
Grade 12 “G”

Submitted to: Signature


Department of Computer
Science (+2)
Table of Contents
Project Structure............................................................................................................................................ 1
Config.php ................................................................................................................................................ 1
OUTPUT ................................................................................................................................................... 1
Header.php ................................................................................................................................................ 2
OUTPUT ................................................................................................................................................... 2
Actions.php ............................................................................................................................................... 2
OUTPUT ................................................................................................................................................... 4
For insert ............................................................................................................................................... 4
For display ............................................................................................................................................ 4
For delete .............................................................................................................................................. 4
Index.php .................................................................................................................................................. 4
Create a html form with different form elements and use buttons for
validation, insertion, update, delete to perform the CRUD ( Create, Read,
Update, Delete ) operation in the data in the database.
1) Insert the data from the form in the table.
2) Retrieve the data from the table and display in list view with edit and delete
buttons.
3) Update the data in the table from form view by clicking edit button.
4) Delete the data in the table from form view by clicking delete button

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";

$conn = new mysqli($server, $username, $password, $dbname);

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>

<button type="submit" name="action" value="insert">Insert</button>


</form>
OUTPUT

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.";
}
}

if ($_POST['action'] == 'delete' && isset($_POST['id'])) {


$id = $_POST['id'];

$sql = "DELETE FROM info WHERE id = '$id'";


if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully<br>";
} else {
echo "Error: " . $conn->error;
}
}

$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');

$sql = "SELECT * FROM info";


$result = $conn->query($sql);

include('header.php');

echo "<h2>User Details</h2>";


echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Address</th>

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>

You might also like