Question

create table cmu_patient
(   patient_id  character varying(13) NOT NULL,
    patient_hn  character varying(7),
    patient_fname   character varying(50),
    patient_lname   character varying(50),
    home_id         integer,
    CONSTRAINT    cmu_patient_pkey PRIMARY KEY (patient_id),
    CONSTRAINT    Fk_home  FOREIGN KEY(home_id)
    REFERENCES    cmu_home(home_id)
);

create table cmu_treatment
(   treatment_id    serial NOT NULL,
    treatment_date  date,
    treatment_time  time without time zone,
    treatment_typecome  character varying(100),
    treatment_detail    text,
    patient_id  character varying(13),
    appointment_id  character varying(5),
    transfer_id character varying(5),
    res_users_id    integer,
    CONSTRAINT    cmu_treatment_pkey PRIMARY KEY (treatment_id),
    CONSTRAINT    Fk_patient  FOREIGN KEY(patient_id)
    REFERENCES    cmu_patient(patient_id),
    CONSTRAINT    Fk_user_id  FOREIGN KEY(res_users_id)
    REFERENCES    res_users(id)
);

$treatment_date = $GET_[...];
$treatment_time = $GET_[...];
$treatment_typecome = $GET_[...];
$treatment_note = $GET_[...];
$CID = $GET_[...];

this code -------- it's incorrect

INSERT INTO cmu_treatment(treatment_id, treatment_date, treatment_time, 
treatment_typecome, treatment_detail, patient_id, appointment_id,transfer_id, res_users_id) 
VALUES(NULL,'".$tratment_date."','".$treatment_time."','". 
$treatment_typecome."','".$treatment_note."','".$CID."',NULL,NULL,NULL) 
WHERE cmu_patient.patient_id = cmu_treatment.patient_id ;

i think that's wrong


i don't know if i want to write insert data into table with where cause i should write sql ?

thank :)

Was it helpful?

Solution

I suspect what you really want is an update, to change existing values in an existing record:

update cmu_treatment
    set treatment_date = $treatment_date,
        treatment_time = $treatment_time,
        treatment_detail = $treatment_typecome,
        treatment_note = $treatment_note
    where patient_id = $CID;

(I'm leaving out the NULL values on the assumption that those shouldn't really change.)

If you do indeed want a new record, you can do:

INSERT INTO cmu_treatment(treatment_id, treatment_date, treatment_time, 
                          treatment_typecome, treatment_detail, patient_id, appointment_id,
                          transfer_id, res_users_id
                         ) 
    select NULL,'".$tratment_date."', '".$treatment_time."','". 
          $treatment_typecome."','".$treatment_note."','".$CID."', NULL, NULL, NULL;

OTHER TIPS

You can write an INSERT statement populating target table with a SELECT statement. In the SELECT statement you can use WHERE condition.

So instead this query:

INSERT INTO table VALUES (....)

You must write:

INSERT INTO table
SELECT fields
FROM anothertable
WHERE condition

In your case, I think you must use an INSERT without WHERE condition if you want to insert only a row in your treatment table.

Tell me if you want to know further info

EDIT After comment

IMHO your statement must be:

INSERT INTO cmu_treatment
(treatment_id, treatment_date, treatment_time, 
treatment_typecome, treatment_detail, patient_id, appointment_id,
transfer_id, res_users_id) 
VALUES
(NULL,'".$tratment_date."','".$treatment_time."',
'".$treatment_typecome."','".$treatment_note."','".$CID."',NULL,NULL,NULL)
INSERT INTO `cmu_treatment`(`treatment_id`, `treatment_date`, `treatment_time`, 
`treatment_typecome`, `treatment_detail`, `patient_id`, `appointment_id`,`transfer_id`, `res_users_id`) 
VALUES(NULL,'".$tratment_date."','".$treatment_time."','". 
$treatment_typecome."','".$treatment_note."','".$CID."',NULL,NULL,NULL) 
WHERE `cmu_patient.patient_id` = `cmu_treatment.patient_id` ;

And you don't need (table name).(column). Is this Inside "" ? If yes then you don't need '".$tratment_date."' you can use only '' so your code will look like this.

INSERT INTO cmu_treatment(treatment_id, treatment_date, treatment_time, 
treatment_typecome, treatment_detail, patient_id, appointment_id,transfer_id, res_users_id) 
VALUES(NULL,'$tratment_date','$treatment_time',' 
$treatment_typecome','$treatment_note','$CID',NULL,NULL,NULL) 
WHERE `patient_id` = patient_id ;

And finally what is patient_id? Is it variable? If not IT MUST BE. Don't give same names to different things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top