[英]How to insert React Array data to Mysql with Axios & Express?
i need to insert a prescription details in mySQL database, but i struggle to inserting it with array data.我需要在 mySQL 数据库中插入处方详细信息,但我很难用数组数据插入它。
I have 2 states, 1 array state to keep the medicines i push into it then send the values to axios when done & 1 state to save the selected medicine.我有 2 个状态,1 个数组 state 以保留我推入其中的药物,然后在完成后将值发送到 axios 和 1 个 state 以保存所选药物。
My options example: using react-select我的选项示例:使用 react-select
const medicine = [
{ id_obat: '123', label: 'Amoxcilin 300mg' },
{ id_obat: '321', label: 'Ibuprofen 500ml' }
];
React codes:反应代码:
const [values, setValues] = useState([]);
const [meds, setMeds] = useState({
obat: {},
cara_pakai: '',
kuantitas: ''
});
const initialState = {
obat: {},
cara_pakai: '',
kuantitas: ''
};
const handleChange = obat => {
setMeds({ ...meds, ['obat']: obat });
};
const addMed = () => {
setValues([...values, meds]);
setMeds({ ...initialState });
};
const onSubmit = e => {
e.preventDefault();
addPrescript(values);
};
My axios action to send it to my API:我的 axios 操作将其发送到我的 API:
export const addPrescript = values => async dispatch => {
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
const form = qs.stringify(values);
try {
const res = await axios.post('/api/prescription', form, config);
dispatch({ type: ADD_PRESCRIPT, payload: res.data });
dispatch(setAlert('Submit Success', 'success', 3000));
} catch (err) {
let errors = err.response.data.errors;
if (errors) {
errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));
}
}
};
My prescription controller:我的处方 controller:
exports.createPrescription = async (req, res) => {
const { id_obat, cara_pakai, kuantitas, deskripsi } = req.body;
const p_value = {
deskripsi: deskripsi
};
const p_sql = 'INSERT INTO resep SET ?';
const d_sql = 'INSERT INTO detail_resep SET ?';
conn.query(p_sql, p_value, (error, p_result) => {
if (error) throw error;
let d_value = [
{
id_resep: p_result.insertId,
id_obat: id_obat,
cara_pakai: cara_pakai,
kuantitas: kuantitas
}
];
conn.query(d_sql, [d_value], (error, d_result) => {
if (error) throw error;
res.status(200).json({
values: d_result
});
});
});
};
My backend server keep sending me error [You have an error in your SQL syntax], i dont know how to solve it.我的后端服务器不断向我发送错误 [您的 SQL 语法有错误],我不知道如何解决。
my API required req.body to submit is |我的 API 需要 req.body 提交是 | id_resep |
id_resep | id_obat |
id_obat | cara_pakai |
cara_pakai | kuantitas |
关提塔斯 | to be success inserted.
要成功插入。 i dont know if im wrong with the stringify.
我不知道我对字符串化是否有误。 my first time work with array data:>
我第一次使用数组数据:>
Any help appreciated.任何帮助表示赞赏。
look like below statement has to change看起来像下面的声明必须改变
const p_sql = 'INSERT INTO resep SET ?';
const d_sql = 'INSERT INTO detail_resep SET ?';
to至
const p_sql = 'INSERT INTO resep( {columnname}) values({value1,value2})';
const d_sql = 'INSERT INTO detail_resep ( {columnname}) values({value1,value2}))';
change {columnname } to your column name and corresponding values in {value}将 {columnname } 更改为您的列名和 {value} 中的相应值
Basically insert statement syntax is not correct here.基本上插入语句语法在这里是不正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.