1

I need to make an insert in the gynecology table, this table has no rows.

Table Gynecology

the session variable is 2 in another page.

$_SESSION ["id_patient"] = 2;


<?php
$insert = $db->prepare("INSERT INTO gynecology(id_patient, nvisita, date_visit, background, reason_visit, exploration, exploration_comp, diagnosis, treatment) 
                                        SELECT IFNULL(gy.id_patient,:id_patient), IFNULL(max(gy.nvisita),0)+1, (now()), :background, :reason_visit, :exploration, :exploration_comp, :diagnosis, :treatment
                                        FROM gynecology gy, patients pa
                                        WHERE gy.id_patient = pa.id
                                        AND PA.ID = :id_patient");

                $insert->bindParam(":background", $background);
                $insert->bindParam(":reason_visit", $reason_visit);
                $insert->bindParam(":exploration", $exploration);
                $insert->bindParam(":exploration_comp", $exploration_comp);
                $insert->bindParam(":diagnosis", $diagnosis);
                $insert->bindParam(":treatment", $treatment);
                $insert->bindParam(":id_patient", $_SESSION["id_patient"]);

                $insert->execute();         
    ?>  

The patients table contains the id of the patient.

thanks.

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69

1 Answers1

0

You are missmatching parameters. Change with this:

            $insert = $db->prepare("INSERT INTO gynecology(id_patient, nvisita, date_visit, background, reason_visit, exploration, exploration_comp, diagnosis, treatment) 
                                    SELECT IFNULL(gy.id_patient,:id_patient), IFNULL(max(gy.nvisita),0)+1, (now()), :background, :reason_visit, :exploration, :exploration_comp, :diagnosis, :treatment
                                    FROM gynecology gy, patients pa
                                    WHERE gy.id_patient = pa.id
                                    AND PA.ID = :id_patient");

            // this first parameter is missing
            $insert->bindParam(":id_patient", $_SESSION["id_patient"]); 
            // the other parameters you have
            $insert->bindParam(":background", $background);
            $insert->bindParam(":reason_visit", $reason_visit);
            $insert->bindParam(":exploration", $exploration);
            $insert->bindParam(":exploration_comp", $exploration_comp);
            $insert->bindParam(":diagnosis", $diagnosis);
            $insert->bindParam(":treatment", $treatment);
            $insert->bindParam(":id_patient", $_SESSION["id_patient"]);
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • What is supposed to do with this comment? – Marcos Pérez Gude Feb 11 '16 at 09:35
  • Please, tell us if this answer solves your problem, and if yes, upvote and check it as correct answer. Thank you :) – Marcos Pérez Gude Feb 11 '16 at 09:41
  • This error is generated: Query failedSQLSTATE[HY000]: General error: 2031 – JCarlos Balaguer Feb 11 '16 at 09:44
  • Try removing `$insert->execute()`. It seems that `bind[*]()` and `execute()` can't be mixed. http://stackoverflow.com/questions/17274556/pdo-error-sqlstatehy000-general-error-2031 – Marcos Pérez Gude Feb 11 '16 at 09:46
  • If you do not inform the WHERE clause if it works but I need to reference the patient id – JCarlos Balaguer Feb 11 '16 at 10:18
  • Háblame en español si quieres, porque no te entiendo. No sé qué es lo que me intentas decir aunque creo que mi respuesta ya te puede ayudar a solventar el problema – Marcos Pérez Gude Feb 11 '16 at 10:21
  • Gracias!! Básicamente necesito hacer un INSERT en la tabla 'gynecology' la cual puede tener muchos registro por cada paciente. En caso de que el paciente no tenga ninguna visita, creará la primera sin embargo, si el paciente ya tiene alguna visita le sumará 1 a la última visita registrada. En la tabla 'patients' está la información de los pacientes con un id para identificarlos. – JCarlos Balaguer Feb 11 '16 at 10:54
  • Cuando intento insertar una nueva visita para un paciente que ya ha tenido una visita, lo hace correctamente. Pero cuando intento insertar una nueva visita para un paciente que no tiene ninguna, me sale el error: SQLSTATE[HY093]: Invalid parameter number Perdona por mi inglés chapurrero. – JCarlos Balaguer Feb 11 '16 at 10:54
  • El error `invalid parameter number` es lo que te explico en la respuesta. Simplemente, falta el primer parámetro de la query que es `id_patient`. Añadiendo eso este error se va. Pero me cuentas que cuando añades esto te sale el error `SQLSTATE[HY000]: General error: 2031` el cual significa que estás mezclando funciones `bindParam()` y `execute()`(más info: http://stackoverflow.com/questions/17274556/pdo-error-sqlstatehy000-general-error-2031 ) – Marcos Pérez Gude Feb 11 '16 at 11:08
  • Lo siento, ha sido un error tonto, pero me has ayudado! Muchas gracias por tu interés. Yo pensaba que podía reutilizar
    $insert->bindParam(":id_patient", $_SESSION["id_patient"]);   
    En más de un apartado del INSERT.
    – JCarlos Balaguer Feb 11 '16 at 11:21
  • Suena lógico, pero PDO necesita que le especifiques los parámetros en orden y todos bien presentados. Si tienes algún otro problema avísame! Por cierto, puedo recomendarte stackoverflow en español: http://es.stackoverflow.com Un saludo! – Marcos Pérez Gude Feb 11 '16 at 11:23
  • genial, muchas gracias :) – JCarlos Balaguer Feb 11 '16 at 11:28