What's new

PHP Php Crud

DMaker

Addict
Joined
Aug 9, 2021
Posts
165
Solutions
1
Reaction
47
Points
114
Baka pwede po magpatulong sa assignment ko, php crud po siya yung libre lang po sana.
 
Sure, I can help you with your PHP CRUD assignment.

CRUD stands for Create, Read, Update, and Delete. It is a common feature in many database-driven web applications. Here are the basic steps you can follow to create a PHP CRUD application:

1. Create a database and table: You can use phpMyAdmin or any other MySQL client to create a database and table for your application.

2. Connect to the database: You need to create a PHP script to connect to your database using PHP's PDO or mysqli extension. This script should be included in all other files that need to interact with the database.

3. Create a form to enter data: You can create a simple HTML form to enter data into your database. This form should submit data to a PHP script that inserts the data into your database.

4. Read data from the database: You can use a SELECT query to retrieve data from your database. You can display this data in a table or in any other format you like.

5. Update data in the database: You can create an edit form that allows users to update data in your database. This form should submit data to a PHP script that updates the data in your database.

6. Delete data from the database: You can create a delete button or link that allows users to delete data from your database. This button should submit data to a PHP script that deletes the data from your database.

Here is a sample PHP CRUD code that you can use as a reference:

Code:
<?php
//Connect to database
$db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

//Insert data into table
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = "INSERT INTO users (name, email) VALUES (:name, :email)";
    $stmt = $db->prepare($query);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->execute();
}

//Read data from table
$query = "SELECT * FROM users";
$stmt = $db->prepare($query);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

//Update data in table
if(isset($_POST['edit'])){
    $id = $_POST['id'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = "UPDATE users SET name = :name, email = :email WHERE id = :id";
    $stmt = $db->prepare($query);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
}

//Delete data from table
if(isset($_POST['delete'])){
    $id = $_POST['id'];
    $query = "DELETE FROM users WHERE id = :id";
    $stmt = $db->prepare($query);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
}
?>

<!-- Display data in table -->
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($users as $user): ?>
            <tr>
                <td><?php echo $user['id']; ?></td>
                <td><?php echo $user['name']; ?></td>
                <td><?php echo $user['email']; ?></td>
                <td>
                    <form method="POST">
                        <input type="hidden" name="id" value="<?php echo $user['id']; ?>">
                        <input type="text" name="name" value="<?php echo $user['name']; ?>">
                        <input type="email" name="email" value="<?php echo $user['email']; ?>">
                        <button type="submit" name="edit">Edit</button>
                    </form>
                    <form method="POST">
                        <input type="hidden" name="id" value="<?php echo $user['id']; ?>">
                        <button type="submit" name="delete">Delete</button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<!-- Form to insert data -->
<form method="POST">
    <input type="text" name="name" placeholder="Name">
    <input type="email" name="email" placeholder="Email">
    <button type="submit" name="submit">Add User</button>
</form>

I hope this helps you with your assignment. Let me know if you have any questions or need further assistance.
 

Similar threads

Back
Top