Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
How can I add a new patient mysql?

I want to insert a new patient, first i have to insert in users but i don't know how insert in patient with a foreign key ('id_user') the id of users that i insert before.

This is my code:

app.post("/api/patient", async function(req, res){
let sql = "INSERT INTO users (email, password, name, surnames, sex, birth_date) VALUES ('"+req.body.email+"','"+req.body.password+"','"+req.body.name+"','"+req.body.surnames+"','"+req.body.sex+"','"+req.body.birth_date+"')";
const user = await query(sql);
var id = user[0].id;
console.log(id);

var sql2 = "INSERT INTO patients (sip, native_language, dependent, id_user) VALUES ('"+req.body.sip+"','"+req.body.native_language+"','"+req.body.dependent+"','"+id+"')";
const patient = await query(sql2);
  response.body = {
    code: 200,
    message: "Solicitud exitosa",
    data: patient
  } 
 res.status(response.body.code);
 res.send(response);
});

And I have the following error:

var id = user[0].id;
TypeError: Cannot read properties of undefined (reading 'id')

I am beginner programmer and I know my code is vulnerable to sql injections.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When executing INSERT or UPDATE statements mysql returns an object containing inserted row id, so: Change var id = user[0].id; to var id = user.insertId;

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda