How to insert data into nested tables through PHP?

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!

Answers


$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 ? )


Need Your Help

How can I use a Shader in XNA to color single pixels?

c# xna shader

I have a standard 800x600 window in my XNA project. My goal is to color each individual pixel based on a rectangle array which holds boolean values. Currently I am using a 1x1 Texture and drawing...