- Open your terminal or command prompt and run:
Core packages install
- Run the command to under the react app:
D:\xampp\htdocs\weblession>cd phpreactcurd - Start the server:
D:\xampp\htdocs\weblession\phpreactcurd>npm start
- We have run this command for bootstrap library:
=> htdocs\weblession\phpreactcurd> npm install bootstrap - Now you can check inside the node_modules folder:
- Now open the app.js file:
- Start the server:
D:\xampp\htdocs\weblession\phpreactcurd>npm start
output
- Create a component folder inside the src/ folder
- Inside the component folder, create the Userlist.js file.
User Table Display
- We want to install the react-router-dom library.
Press:- ctr + c (Stop Server)
=> htdocs\weblession\phpreactcurd> npm install react-router-dom - Now we want to import the Dom library

- BrowserRouter: It represents the router for your application, and it is a key component of React Router that provides routing functionality to react application.
- Routes: The routes component is typically used to define a collection of routes.
- Route: The route component is used to define an individual route in your application.
- Link: Link component is used to creating hyperlink that navigates to different parts of your application.
Note:- All components of this React Router DOM library.
Fetch Data From Mysql
- useEffect:- This useEffect is a React hook that allows us to perform side effects in our component, and these side effects we can use for data fetching or for DOM manipulation
- useState:- useState hook is also a React hook which enables functional components to manage local statge we can use this use statement to create or update a state variable within a functional component.
- Created a new directory with the name API, and under this directory, we have created a PHP file with the name action.php.
action.php
output:-
Insert Data in Mysql
Step 1:- Make Form & Set Route- In Userlist.js, added one button with the name of add and set the route.
- Now we want to import the useNavigate hook.
- useNavigate:- This hook for use navigate from one route to another route.

Step 2:- Created a new directory with the name API, and under this directory, we have created a PHP file with the name action.php.
Action.php
Update Data in Mysql
Step 1:- Make Form & Set Router for Edit Data Component
Make Route:
- Make Route:- App.js
import Edit from './Component/Edit';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/edit/:user_id" element={<Edit />} />
</Routes>
</BrowserRouter>
);
}
export default App;
- Edit Button:- Added the link in the Action column.
Userlist.js
Userlist.js
<td>
<Link to={`/edit/${user.id}`} className="btn btn-warning btn-sm">Edit</Link>
</td>
- Target URL:- when click on link to call API.
Edit.js
const apiUrl =
`http://localhost/weblession/phpreactcurd/api/action.php?id=${user_id}`;
- View All Button:- Added the link in the Action column.
Edit.js
Edit.js
const apiUrl =
`http://localhost/weblession/phpreactcurd/api/action.php?id=${user_id}`;
Edit.js
<div className="col-md-6">
<Link to="/" className="btn btn-success btn-sm float-end">View All</Link>
</div>
Make a form and fetch data
Edit.js
import React, {useState, useEffect } from 'react';
import { Link, useNavigate, useParams } from 'react-router-dom';
function Edit() {
const {user_id} = useParams(); // get id from URL
//console.log({user_id})
//hold data
const [user, setUser] = useState({
first_name : '',
last_name : '',
email : '',
});
const handleChange = (event) => {
const {name, value} = event.target;
setUser({
...user, // spread operator wit user variable
[name] : value
})
}
//fetch data from mysql database
const fetchUserData = () => {
fetch(`http://localhost/weblession/phpreactcurd/api/action.php?id=${user_id}`)
.then((response) => response.json())
.then((data) => {
//console.log(data);
setUser(data);
});
};
useEffect(() => {
fetchUserData();
},[]);
//console.log(user);
return(
<div className = "card">
<div className = "card-header">
<div className = "row">
<div className="col-md-6">Edit User</div>
<div className="col-md-6">
<Link to="/" className="btn btn-success btn-sm float-end">
View All</Link>
</div>
</div>
</div>
<div className="card-body">
<div className="row">
<div className="col-md-4"> </div>
<div className="col-md-4">
<form method = "POST">
<div className="mb-3">
<label>First Name</label>
<input type="text"
name="first_name"
className="form-control"
value={user.first_name}
onChange={handleChange} />
</div>
<div className="mb-3">
<label>Last Name</label>
<input type="text" name="last_name" className="form-control" value={user.last_name} onChange={handleChange} />
</div>
<div className="mb-3">
<label>Email</label>
<input type="email" name="email" className="form-control" value={user.email} onChange={handleChange} />
</div>
<div className="mb-3">
<input type="submit" className="btn btn-primary" value="Edit" />
</div>
</form>
</div>
</div>
</div>
</div>
);
}
export default Edit;
Step 2:- Created a new directory with the name API, and under this directory, we have created a PHP file with the name action.php.Action.php.
<?php
Make Update Data API
Structure:-
Edit.js.
Action.php.
<?php
header("Access-Control-Allow-Methods: 'DELETE,GET,HEAD,OPTIONS,PUT,POST,PATCH'");
Create Delete Data API
- Delete Button:- We want to add the Delete button.
Userlist.js
- Create button:-
<button type="button" onClick={() => handleDelete(user.id)} className="btn btn-danger btn-sm">Delete</button>
- Call button:-











No comments:
Post a Comment
If you have any problem please let me know.