Crud operation useing html,js,php

 To add, edit, and delete records in a database using PHP, MySQL (which is typically managed via phpMyAdmin), and JavaScript, you would typically follow these steps:


1. **Setting Up the Database:** Create a MySQL database and table(s) via phpMyAdmin.


2. **PHP Scripts:** Write PHP scripts to handle database operations (add, edit, delete) using MySQL queries. These scripts will interact with the database.


3. **HTML/JS Frontend:** Create a frontend interface (HTML/JS) where users can input data and interact with the database.


4. **AJAX Requests:** Use JavaScript to send AJAX requests to the PHP scripts to perform CRUD operations without reloading the page.


Here's a basic example:


**HTML (index.html):**

```html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>CRUD Operations</title>

</head>

<body>

    <h2>Add Record</h2>

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

    <input type="text" id="email" placeholder="Email">

    <button onclick="addRecord()">Add</button>


    <h2>Edit Record</h2>

    <input type="text" id="editId" placeholder="ID">

    <input type="text" id="editName" placeholder="Name">

    <input type="text" id="editEmail" placeholder="Email">

    <button onclick="editRecord()">Edit</button>


    <h2>Delete Record</h2>

    <input type="text" id="deleteId" placeholder="ID">

    <button onclick="deleteRecord()">Delete</button>


    <script src="script.js"></script>

</body>

</html>

```


**JavaScript (script.js):**

```javascript

function addRecord() {

    var name = document.getElementById("name").value;

    var email = document.getElementById("email").value;

    // Send AJAX request to PHP script to add record

}


function editRecord() {

    var id = document.getElementById("editId").value;

    var name = document.getElementById("editName").value;

    var email = document.getElementById("editEmail").value;

    // Send AJAX request to PHP script to edit record

}


function deleteRecord() {

    var id = document.getElementById("deleteId").value;

    // Send AJAX request to PHP script to delete record

}

```


**PHP (backend.php):**

```php

<?php

// Include database connection file

include_once("db.php");


// Add record

if(isset($_POST['name']) && isset($_POST['email'])) {

    $name = $_POST['name'];

    $email = $_POST['email'];

    // Perform database query to add record

}


// Edit record

if(isset($_POST['editId']) && isset($_POST['editName']) && isset($_POST['editEmail'])) {

    $id = $_POST['editId'];

    $name = $_POST['editName'];

    $email = $_POST['editEmail'];

    // Perform database query to edit record

}


// Delete record

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

    $id = $_POST['deleteId'];

    // Perform database query to delete record

}

?>

```


Remember to replace the comments with actual MySQL queries to perform CRUD operations on the database. Additionally, make sure to sanitize user inputs to prevent SQL injection attacks.



In JavaScript, you don't perform SQL queries directly; instead, you use AJAX to send a request to a server-side script (usually written in PHP, Python, Node.js, etc.) that interacts with the database. Here's how you can execute a SELECT query using JavaScript and AJAX:


**HTML (index.html):**

```html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>SELECT Query</title>

</head>

<body>

    <div id="output"></div>

    

    <script src="script.js"></script>

</body>

</html>

```


**JavaScript (script.js):**

```javascript

window.onload = function() {

    fetchRecords();

};


function fetchRecords() {

    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {

        if (this.readyState == 4 && this.status == 200) {

            displayRecords(JSON.parse(this.responseText));

        }

    };

    xhttp.open("GET", "fetch_records.php", true); // Replace with your server-side script

    xhttp.send();

}


function displayRecords(records) {

    var outputDiv = document.getElementById("output");

    var html = "<h2>Records</h2>";

    html += "<ul>";

    records.forEach(function(record) {

        html += "<li>" + record.name + " - " + record.email + "</li>"; // Assuming name and email are fields in your database

    });

    html += "</ul>";

    outputDiv.innerHTML = html;

}

```


**PHP (fetch_records.php):**

```php

<?php

// Include database connection file

include_once("db.php");


// Perform SELECT query

$query = "SELECT * FROM your_table"; // Replace "your_table" with your actual table name

$result = mysqli_query($connection, $query);


if(mysqli_num_rows($result) > 0) {

    $rows = array();

    while($row = mysqli_fetch_assoc($result)) {

        $rows[] = $row;

    }

    echo json_encode($rows);

} else {

    echo "No records found.";

}


// Close connection

mysqli_close($connection);

?>

```


Make sure to replace `"your_table"` with the actual table name and `"db.php"` with your database connection file in both JavaScript and PHP code. This setup will fetch records from the database using AJAX and display them on the HTML page.

Previous
Next Post »