[go: up one dir, main page]

0% found this document useful (0 votes)
50 views3 pages

Crude PHP

Uploaded by

Muhammad Junaid
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)
50 views3 pages

Crude PHP

Uploaded by

Muhammad Junaid
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/ 3

In PHP, crude operations refer to the basic CRUD operations — Create, Read, Update, and

Delete — that are typically performed on a database, often using MySQL. Below is a simple
example that demonstrates how to implement these operations in PHP.

🖥️PHP CRUD Example with MySQL


🔧 Database Setup

Create a database called crud_example and a table called users:

CREATE DATABASE crud_example;

USE crud_example;

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);

📄 PHP Code for CRUD Operations

🔧 Database Connection (db.php)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "crud_example";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

📝 1. Create Operation (Insert Data)

<?php
include 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];

$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";


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

<form method="post">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
<input type="submit" value="Add User">
</form>

📖 2. Read Operation (Display Data)

<?php
include 'db.php';

$sql = "SELECT * FROM users";


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

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row['id'] . " - Name: " . $row['name'] . " - Email: " .
$row['email'] . "<br>";
}
} else {
echo "No records found.";
}
?>

✏️3. Update Operation (Edit Data)

<?php
include 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];

$sql = "UPDATE users SET name='$name', email='$email' WHERE id=$id";


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

<form method="post">
ID: <input type="number" name="id" required><br>
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
<input type="submit" value="Update User">
</form>

❌ 4. Delete Operation (Remove Data)

<?php
include 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST['id'];

$sql = "DELETE FROM users WHERE id=$id";


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

<form method="post">
ID: <input type="number" name="id" required><br>
<input type="submit" value="Delete User">
</form>

🧪 Test the CRUD Operations

1. Run the code on a local server (like XAMPP or MAMP).


2. Access the forms via the browser.
3. Perform Create, Read, Update, and Delete operations.

You might also like