1

I think this is something related to PDO.

this is my patientinfo table

patientid | name | age | email | address

and this is my remarks tables

patientid | remarksid | date | description

I'd like to INSERT data to the patientinfo and to the remarks table where patientid of both tables will be synchronized.

The problem is I dont know how to query this. This is what I do but it gives me an error.

$query = "INSERT INTO patientinfo (name, age, email, address)
              VALUES (:name, :age, :email, :address);";

    $query_params = array(
            ':name'     => $_POST['name'],
            ':age'      => $_POST['age'],
            ':email'    => $_POST['email'],
            ':address'  => $_POST['address'],
    );

    $query = "INSERT INTO remarks (patient_id, description) VALUES (:patient_id, :remarks) WHERE remarks.patient_id = patientinfo.patient_id;";
    $query_params = array(':remarks' => $_POST['remarks']);

    try{
        $stmt = $dbname->prepare($query);
        $result = $stmt->execute($query_params);
    }

    catch(PDOException $ex){
            $response["success"] = 0;
            $response["message"] = $ex ;

            die(json_encode($response));
    }

i made patientid in the patientinfo AUTOINCREMENT. PLEASE! THANK YOU SO MUCH FOR YOUR HELP!

8
  • 3
    Please don't confuse PHPMyAdmin and MySQL. Commented Feb 6, 2014 at 12:04
  • Are you trying to INSERT data or SELECT data, or both? Commented Feb 6, 2014 at 12:06
  • both, i'd like to INSERT data to these tables and at the same time SELECT the patientid from patientinfo and INSERT it to the remarks' patientid. :) Commented Feb 6, 2014 at 12:08
  • saying shortly: you can't Commented Feb 6, 2014 at 12:10
  • 1
    >> @Quentin im not using mysql :) – superJen 16 secs ago // Are you sure ? :) Commented Feb 6, 2014 at 12:11

1 Answer 1

1
$query = "INSERT INTO patientinfo (name, age, email, address)
              VALUES (:name, :age, :email, :address);";

$query_params = array(
        ':name'     => $_POST['name'],
        ':age'      => $_POST['age'],
        ':email'    => $_POST['email'],
        ':address'  => $_POST['address'],
);

try{
    $stmt = $dbname->prepare($query);
    $stmt->execute($query_params);

    $patient_id = $dbname->lastInsertId();

    $query = "INSERT INTO remarks (patientid, description) VALUES (:patient_id, :remarks)";
    $query_params = array(':remarks' => $_POST['remarks'],':patient_id'=>$patient_id);

    $q = $dbname->prepare($query); 
    $q->execute($query_params);

}catch(PDOException $ex){
        $response["success"] = 0;
        $response["message"] = $ex ;

        die(json_encode($response));
}

You should write something like that. Check column names please(patientid or patient_id ? )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.