Mysql and SQL
Mysql and SQL
What is CRUD?
CRUD stands for Create, Read, Update, and Delete – the four basic operations that can be performed on
a database.
• Create (INSERT): Adds new records to a table.
• Read (SELECT): Retrieves data from a table.
• Update (UPDATE): Modifies existing records.
• Delete (DELETE): Removes records from a table.
Example CRUD operations in SQL:
-- Create a new record
INSERT INTO users (name, email) VALUES ('James Mwanza, '[email protected]');
-- Read records
SELECT * FROM users;
-- Update a record
UPDATE users SET email = '[email protected]' WHERE name = 'James Mwanza;
-- Delete a record
DELETE FROM users WHERE name = 'James Mwanza;
Command Description
Command Description
UPDATE tablename SET column1 = 'value' WHERE condition; Updates existing data.
Command Description
SELECT column1, COUNT(*) FROM tablename GROUP BY Groups data and returns aggregate
column1; counts.
SELECT column1 FROM tablename WHERE column2 LIKE Searches for a pattern within a
'%value%'; column.
<?php
$conn = new mysqli("localhost", "root", "", "my_database");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $conn->real_escape_string($_POST['name']);
$email = $conn->real_escape_string($_POST['email']);
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
if ($conn->query($sql) === TRUE) {
echo "<script>alert('New record created successfully.');
window.location.href='display.php';</script>";
} else {
echo "Error: " . $conn->error;
}
}
$conn->close();
?>