Please anyone help me I don’t have a time :
I write my react app to save the data in database by php but it give me this error in console and don’t save in database (I use xxamp): and the name of the coloms in tbl table are isConfirmed and coursename
**
POST http://localhost:3000/Components/saveCourses.php 404 (Not Found)
App.js:46 Fetch failed loading: POST “http://localhost:3000/Components/saveCourses.php”. **
enter image description here
this is my app.js :
import React, { useState, useEffect } from 'react';
import CourseForm from './Components/CourseForm';
import CourseList from './Components/CourseList.js';
const App = () => {
const [courses, setCourses] = useState([]);
useEffect(() => {
fetchData();
}, []);
const fetchData = async () => {
try {
const response = await fetch('./Components/show.php');
const data = await response.json();
setCourses(data);
} catch (error) {
console.log('Error:', error);
}
};
const addCourse = (courseName) => {
setCourses([...courses, { name: courseName, isConfirmed: false }]);
};
const editCourse = (index) => {
const newCourseName = prompt("Enter the new value:");
if (newCourseName) {
setCourses(courses.map((course, i) => i === index ? { ...course, name: newCourseName } : course));
}
};
const deleteCourse = (index) => {
setCourses(courses.filter((_, i) => i !== index));
};
const confirmCourse = (index) => {
setCourses(courses.map((course, i) => i === index ? { ...course, isConfirmed: true } : course));
};
const saveCourses = async () => {
const confirmSave = window.confirm("Are you sure you want to save the data?");
if (confirmSave) {
try {
await fetch('./Components/saveCourses.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(courses)
});
alert("Courses saved successfully!");
} catch (error) {
console.error('Error:', error);
}
} else {
alert("Save operation cancelled.");
}
};
return (
<div className="App">
<CourseForm addCourse={addCourse} />
<CourseList courses={courses} editCourse={editCourse} deleteCourse={deleteCourse} confirmCourse={confirmCourse} />
<button onClick={saveCourses}>Save DB</button>
<button onClick={fetchData}>Show Data</button>
<div id="dataContainer"></div>
</div>
);
};
export default App;
and this is the php file :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "courses";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
}
$data = file_get_contents("php://input");
$ddd = json_decode($data, true);
$values = '';
foreach ($ddd as $row) {
$values .= "('$row[name]', $row[isConfirmed]),";
}
$values = rtrim($values, ',');
$insertSql = "INSERT INTO tbl (coursename, isConfirmed) VALUES " . $values;
if (mysqli_query($conn, $insertSql)) {
echo "Records saved successfully.";
} else {
echo "Error: " . $insertSql . "<br>" . mysqli_error($conn);
}
I want to solve the error or give me anything to help me
Qoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.