Wie füge ich Daten aus einem React-Formular in MySQL ein?
Posted: 20 Jan 2025, 15:32
Ich versuche, meine erste React-Anwendung zu entwickeln. Das Backend funktioniert ordnungsgemäß. Ich weiß jedenfalls nicht, wie ich die eingegebenen Daten in einem Reaktionsformular in meine Datenbank einfügen soll. Ich habe es mit Axios versucht, hatte aber viele Probleme. Hier ist mein Code:
Backend:
Frontend:
Vielen Dank für Ihre Hilfe!
Backend:
Code: Select all
router.post('/insert',(req,res) =>{
jsondata = req.body;
description = jsondata['description'];
distance = jsondata['distance'];
hours = jsondata['hours'];
minutes = jsondata['minutes'];
seconds = jsondata['seconds'];
conn.query('INSERT INTO run (description, distance, hours, minutes, seconds) VALUES (?,?,?,?,?)', [description,distance,hours,minutes,seconds], (err) =>{
if(err)
res.send(err)
if(!err)
res.send("Insert succeded.")
})
})
Code: Select all
import React, {useEffect, useState} from 'react';
import {Link} from 'react-router-dom';
function Insert(){
return(
Insert the details of your run
Submit
);
}
export default Insert;