0% found this document useful (0 votes)
66 views8 pages

CRUD Operations

The document describes implementing CRUD operations using PHP and MySQL. It involves: 1) Creating a PHP file to connect to a MySQL database and handle HTTP requests. 2) Defining a table with required attributes to store and retrieve data. 3) Adding, viewing, updating, and deleting records through a basic HTML interface. 4) Testing the CRUD operations by manipulating data in the users table.

Uploaded by

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

CRUD Operations

The document describes implementing CRUD operations using PHP and MySQL. It involves: 1) Creating a PHP file to connect to a MySQL database and handle HTTP requests. 2) Defining a table with required attributes to store and retrieve data. 3) Adding, viewing, updating, and deleting records through a basic HTML interface. 4) Testing the CRUD operations by manipulating data in the users table.

Uploaded by

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

CRUD operations

Aim:

To implement CRUD operations using mysql and php

Algorithm:

step1: Start the process.

step2: Create a PHP file named "crud.php."

step 3: Set up a database connection.

step 4: Create a table "users" with the required Attributes.

step 5: Handle HTTP POST requests for CRUD operations.

step 6: Create a basic HTML page with textbox, button.

step 7: Implement basic client-side validation to ensure data integrity


(e.g., check for empty fields).

step 8: Test your CRUD operations by adding, viewing, updating, and


deleting records through the user interface.

step 9: Implement basic error handling:

step 10:Stop the process.


Program:

<?php

// Database configuration

// Create a database connection

$mysqli = new mysqli("localhost","root","","crud_db"));

// Check the connection

if ($mysqli->connect_error) {

die("Connection failed: " . $mysqli->connect_error);

// Create operation

if (isset($_POST['create'])) {

$name = $_POST['name'];

$email = $_POST['email'];

$stmt = $mysqli->prepare("INSERT INTO users (name, email) VALUES


(?, ?)");
$stmt->bind_param("ss", $name, $email);

if ($stmt->execute()) {

echo "Record created successfully.";

} else {

echo "Error: " . $stmt->error;

$stmt->close();

// Read operation (Retrieve data from the database)

$users = array();

$result = $mysqli->query("SELECT * FROM users");

if ($result->num_rows > 0) {

while ($row = $result->fetch_assoc()) {

$users[] = $row;

}
// Update operation

if (isset($_POST['update'])) {

$id = $_POST['id'];

$newName = $_POST['new_name'];

$newEmail = $_POST['new_email'];

$stmt = $mysqli->prepare("UPDATE users SET name=?, email=?


WHERE id=?");

$stmt->bind_param("ssi", $newName, $newEmail, $id);

if ($stmt->execute()) {

echo "Record updated successfully.";

} else {

echo "Error: " . $stmt->error;

$stmt->close();

}
// Delete operation

if (isset($_POST['delete'])) {

$id = $_POST['id'];

$stmt = $mysqli->prepare("DELETE FROM users WHERE id=?");

$stmt->bind_param("i", $id);

if ($stmt->execute()) {

echo "Record deleted successfully.";

} else {

echo "Error: " . $stmt->error;

$stmt->close();

// Close the database connection

$mysqli->close();

?>
<!DOCTYPE html>

<html>

<head>

<title>CRUD Example with MySQLi</title>

</head>

<body>

<h1>CRUD Example with MySQLi</h1>

<!-- Create operation form -->

<h2>Create Record</h2>

<form method="post">

<input type="text" name="name" placeholder="Name" required>

<input type="email" name="email" placeholder="Email" required>

<input type="submit" name="create" value="Create">

</form>

<!-- Read operation (Display data) -->

<h2>Read Records</h2>

<table border="1">

<tr>
<th>ID</th>

<th>Name</th>

<th>Email</th>

<th>Actions</th>

</tr>

<?php foreach ($users as $user): ?>

<tr>

<td><?= $user['id']; ?></td>

<td><?= $user['name']; ?></td>

<td><?= $user['email']; ?></td>

<td>

<form method="post">

<input type="hidden" name="id" value="<?= $user['id']; ?


>">

<input type="text" name="new_name" placeholder="New


Name" required>

<input type="email" name="new_email" placeholder="New


Email" required>

<input type="submit" name="update" value="Update">

<input type="submit" name="delete" value="Delete">

</form>
</td>

</tr>

<?php endforeach; ?>

</table>

</body>

</html>

Output

Result:

Thus the program has been executed successfully.

You might also like