I'm trying to post input data from useState to Mysql but it's not happening. While I am able to get the data from the database to the page, when it comes to publishing, I don't know how to send the data from the status hook to mysql when there are a lot of values. Please take a look
My code for sending form data
import React,{useState , useRef} from "react"; import "./AddProjects.css" import axios from "axios"; import Projects from "../Projects/Projects"; import {BiChevronLeftCircle,BiChevronRightCircle} from "react-icons/bi" export default function AddProjects() { const [AddProjects,setAddProjects] = useState({ ProjectName:"", Discription:"", git:"", Img:"" }) const [getQuery,setGetQuery] = useState({ projectList:[] }) const inputFile = useRef(null) function handleChange(e) { const {name , value} = e.target; setAddProjects(newProjects => ({ ...newProjects,[name]:value })) } function imgChange(e) { setAddProjects({ ...AddProjects, Img: URL.createObjectURL(e.target.files[0]) }); } function getProjectList() { axios.get(`http://localhost:3060/projects`) .then((response) => response.data) .then(response2 => { setGetQuery({projectList : response2}) }) } function onSubmitHandler(e) { axios.post(`http://localhost:3060/addProjects`,{ ProjectName: AddProjects.ProjectName, Discription:AddProjects.Discription, git:AddProjects.git, Img:AddProjects.Img }) getProjectList() } return( <> <div className="AddProjects"> <form onSubmit={onSubmitHandler} method="POST" encType="multipart/form-data" > <h2>Add New Project</h2> <input type="text" placeholder="Project Name..." name="ProjectName" onChange={handleChange}/> <input type="text" placeholder="Project Discription..." name="Discription" onChange={handleChange}/> <input type="text" placeholder="Git Repository/Code..." name="git" onChange={handleChange}/> <input type="file" accept="image/jpg,image/jpeg" name="Img" onChange={imgChange} ref={inputFile} /> <button type="button"onClick={() => inputFile.current.click()}>Add New Image</button> <button type="submit" >Submit Project</button> </form> <button onClick={getProjectList}>click me </button> </div> <div> <div className="Section-Projects" > <h1>My Projects </h1> {/* <Link to={checkBox?'/AddProjects':""} ><button className="Add-newProject" onClick={onStateChange}><IoIosAddCircleOutline/></button></Link> */} <div className="Projects"> <button className="arrowLeft"><BiChevronLeftCircle /></button> <div className="Single"> {getQuery.projectList.map((gettingQuery) => <Projects ProjectId={gettingQuery.ProjectId} ProjectName={gettingQuery.ProjectName} Discription={gettingQuery.Discription} git={gettingQuery.git} Img={gettingQuery.Img} /> )} </div> <button className="arrowRight"><BiChevronRightCircle /></button> </div> </div> </div> </> ) };
As you can see, I want to send 4 things to a mysql table but I don't think this is the way to do it, I just can't figure it out
The code of the file that is sending data
const express = require('express'); const cors = require('cors') const PORT = 3060; const db = require('./Database') const bodyParser = require('body-parser'); const { response } = require('express'); const app = express(); app.use(cors()); app.use(bodyParser.json()); app.get('/projects', (req,res) => { const TASK_QUERY = `SELECT * FROM addprojects.newproject`; db.query(TASK_QUERY , (err, response) => { if(err) res.status(404).send('somthings wrong') else res.send(response) }) }); app.post('/addProjects', (req,res) => { const ADD_QUERY = `INSERT INTO addprojects.newproject VALUES('${req.body.data}')`; db.query(ADD_QUERY, (err) => { if (err) console.log(err) else res.send('Query added sucsessfully') }) } ); app.listen(PORT, () => { console.log('app is listning to port 3000') })
I think although the posting method is correct, the value is not so please help me, I've been stuck on this for 2 days Any advice would be helpful
For backend:
You don't mention column names, it doesn't know which field belongs to which column in the table.
For the front end:
ProjectName,Discription,git,Img Make these key names exactly the same as your column names. and you are uploading image in this api so in formdata convert the data from front and then pass that data in api and in backend you will pass that data in formdata