Lädt...


🔧 Building a Simple CRUD Application with PHP and MySQL


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

PHP and MySQL have been my go-to for developing dynamic web applications. If you're just getting started or want to build a simple app that manages data, mastering CRUD operations (Create, Read, Update, Delete) is the first step. Let me walk you through how to build a basic CRUD application using PHP and MySQL. I'll break it down in a way that worked for me when I started, and I hope it helps you too.

Step 1: Setting Up the Environment

Before we jump into coding, you'll need a development environment. Personally, I use XAMPP because it simplifies the setup process. If you haven’t already, download and install XAMPP or WAMP. These local servers bundle Apache, PHP, and MySQL, so you don’t have to install them individually. Once you've got that running, we can start building.

Step 2: Creating the Database

First, we need to set up the database in MySQL. I use phpMyAdmin for this but feel free to use any tool you're comfortable with. Here’s a simple SQL script to create our database and table:

CREATE DATABASE crud_app;
USE crud_app;

CREATE TABLE users (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

This will create a crud_app database and a users table with id, name, email, and created_at fields, where we’ll store the information.

Step 3: Connecting PHP to MySQL

Okay, now let’s connect PHP to the database. It’s an important step for any app that uses a database. Here’s how you can do it with mysqli:

<?php
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'crud_app';

$conn = new mysqli($host, $user, $password, $database);

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

This snippet creates a connection between PHP and MySQL, which will allow us to interact with our database.

Step 4: Building the CRUD Operations

1. Create (Inserting Data)

To start adding data to the database, create a form that captures user input and sends it via a POST request to a PHP script that handles the insertion.

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];

    $query = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
    $result = $conn->query($query);

    if ($result) {
        echo "Record added successfully!";
    } else {
        echo "Error: " . $conn->error;
    }
}
?>
<form method="post" action="">
  Name: <input type="text" name="name" required>
  Email: <input type="email" name="email" required>
  <button type="submit" name="submit">Submit</button>
</form>

2. Read (Displaying Data)

To display the data stored in the database, run a simple SELECT query and loop through the results like this:

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

while ($row = $result->fetch_assoc()) {
    echo $row['name'] . ' - ' . $row['email'] . '<br>';
}

3. Update (Editing Data)

For updating data, first, fetch the record and pre-fill it into a form for editing. After submission, the updated info is saved back to the database:

if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $email = $_POST['email'];

    $query = "UPDATE users SET name='$name', email='$email' WHERE id=$id";
    $result = $conn->query($query);

    if ($result) {
        echo "Record updated successfully!";
    } else {
        echo "Error: " . $conn->error;
    }
}
?>
<form method="post" action="">
  <input type="hidden" name="id" value="<?php echo $user['id']; ?>">
  Name: <input type="text" name="name" value="<?php echo $user['name']; ?>" required>
  Email: <input type="email" name="email" value="<?php echo $user['email']; ?>" required>
  <button type="submit" name="update">Update</button>
</form>

4. Delete (Removing Data)

Finally, to delete data, create a button that sends a DELETE query.

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

    $query = "DELETE FROM users WHERE id=$id";
    $result = $conn->query($query);

    if ($result) {
        echo "Record deleted successfully!";
    } else {
        echo "Error: " . $conn->error;
    }
}
?>
<form method="post" action="">
  <input type="hidden" name="id" value="<?php echo $user['id']; ?>">
  <button type="submit" name="delete">Delete</button>
</form>

With these steps, you now have a basic CRUD application using PHP and MySQL. This example can easily be expanded to include more complex features like additional fields, improved validation, or even functionalities like pagination and file uploads. Whether you're looking to manage dynamic data for a personal project or scale up for a larger application, this CRUD framework sets the foundation.

If you're looking for more help or want to build something bigger, feel free to reach out to me. I love working on projects that push boundaries and take simple ideas to the next level. Check out my GitHub or my portfolio website for more of my work. You can also drop me a message at [email protected]. Let's build something awesome together! 🫶🏽

...

🔧 Building a Simple CRUD Application with PHP and MySQL


📈 43.43 Punkte
🔧 Programmierung

🔧 Building a CRUD Application with Node.js, Express, and MySQL


📈 33.8 Punkte
🔧 Programmierung

🔧 Building a Simple CRUD Application with React and PostgreSQL using Docker


📈 33.37 Punkte
🔧 Programmierung

🕵️ rConfig up to 3.9.4 lib/crud/search.crud.php Command privilege escalation


📈 31.26 Punkte
🕵️ Sicherheitslücken

🔧 Creating a CRUD Application with PHP, MySQL, and Lithe


📈 30.81 Punkte
🔧 Programmierung

🔧 FastAPI Beyond CRUD Part 3 - Buiding a CRUD REST API


📈 28.27 Punkte
🔧 Programmierung

🔧 FastAPI Beyond CRUD Part 6 - CRUD With Async SQLModel (An Introduction to Dependency Injection)


📈 28.27 Punkte
🔧 Programmierung

🔧 Introducing saksh-crud: Simplify Your Node.js CRUD Operations


📈 28.27 Punkte
🔧 Programmierung

🔧 Creating a CRUD Application with Golang and MySQL


📈 27.82 Punkte
🔧 Programmierung

🔧 Building Simple CRM with Vue: Document CRUD Operations


📈 26.76 Punkte
🔧 Programmierung

🔧 Building a Simple CRUD Service with Pylon


📈 26.76 Punkte
🔧 Programmierung

🔧 Building a CRUD Application with Node.js, Express, and MongoDB


📈 26.73 Punkte
🔧 Programmierung

🔧 Building a User CRUD Application with Spring Boot and Docker


📈 26.73 Punkte
🔧 Programmierung

🔧 Creating a Simple CRUD Application with Vue.js


📈 26.09 Punkte
🔧 Programmierung

🔧 Mastering CRUD Operations with PHP and MySQL


📈 25.5 Punkte
🔧 Programmierung

🔧 Building a CRUD Application with the MERN Stack: A Step-by-Step Guide


📈 25.43 Punkte
🔧 Programmierung

🔧 Building a CRUD Application with Ruby on Rails: Step-by-Step Guide


📈 25.43 Punkte
🔧 Programmierung

🔧 Criando uma Aplicação CRUD com PHP, MySQL e Lithe


📈 24.2 Punkte
🔧 Programmierung

🔧 CRUD PHP e MySQL e API e Fundamentos importantes


📈 24.2 Punkte
🔧 Programmierung

🔧 CRUD Simples Utilizando PHP + MySql + Bootstrap 4


📈 24.2 Punkte
🔧 Programmierung

🕵️ CVE-2023-48078 | Simple CRUD Functionality 1.0 add.php title sql injection


📈 23.77 Punkte
🕵️ Sicherheitslücken

🔧 🚀 Build a Simple CRUD API in PHP with the Flight Framework


📈 23.77 Punkte
🔧 Programmierung

🔧 Building a Fast and Flexible CRUD API with Node.js and MongoDB Native Drivers


📈 22.72 Punkte
🔧 Programmierung

🔧 Build CRUD RESTful API Using Spring Boot 3, Spring Data JPA, Hibernate, and MySQL Database


📈 22.51 Punkte
🔧 Programmierung

🔧 Setup CRUD App: Express, Typescript and MySQL


📈 22.51 Punkte
🔧 Programmierung

🎥 Simplify CRUD operations with REPLACE INTO in MariaDB and MySQL


📈 22.51 Punkte
🎥 Video | Youtube

🔧 CRUD With Flask And MySql #1 Introduction


📈 22.51 Punkte
🔧 Programmierung

🔧 CRUD With Flask And MySql #2 Prepare


📈 22.51 Punkte
🔧 Programmierung

🔧 Build a Simple CRUD App with Spring Boot and Vue.js


📈 22.08 Punkte
🔧 Programmierung

🔧 A Simple Crud App With Prisma, Express, and PostgreSQL


📈 22.08 Punkte
🔧 Programmierung

🔧 A Simple CRUD app With GraphQL, Apollo Server, MongoDB, and Express


📈 22.08 Punkte
🔧 Programmierung

🔧 Basic CRUD API using Django and Django REST Framework (DRF) || Simple API for managing a list of books.


📈 22.08 Punkte
🔧 Programmierung

🔧 How to C#: Creating a simple CRUD page with Blazor and EntityFrameworkCore


📈 22.08 Punkte
🔧 Programmierung

🔧 Building a CRUD App with Next.js, React Query, React Hook Form, and Yup


📈 21.42 Punkte
🔧 Programmierung

matomo