Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

php - Get value from ForeignKey to other table and insert on the new one

I have a form for patients that I fill with the name, contact, gender and then doctor_id and nurse_id, they are declared as FK on phpmyadmin and the value is there if I insert it manually, but i cannot retrieve the value of the FK to insert it on the form and save on patients table. Here are my create function and php with form.

function create(){

    if($this->isAlreadyExist()){
        return false;
    }
    $query = "INSERT INTO  ". $this->table_name ." 
                    (`name`, `phone`, `gender`, `health_condition`, `doctor_id`, `nurse_id`, `created`)
              VALUES
                    ('".$this->name."', '".$this->phone."', '".$this->gender."', '".$this->health_condition."', '".$this->doctor_id."',  '".$this->nurse_id."', '".$this->created."')";

    $stmt = $this->conn->prepare($query);

    if($stmt->execute()){
        $this->id = $this->conn->lastInsertId();
        return true;
    }

    return false;
}

The create.php

<?php


include_once '../config/database.php';
include_once '../objects/patient.php';

$database = new Database();
$db = $database->getConnection();
 
$patient = new Patient($db);
 
$patient->name = $_POST['name'];
$patient->phone = $_POST['phone'];
$patient->gender = $_POST['gender'];
$patient->health_condition = $_POST['health_condition'];
$patient->doctor_id = $_POST['doctor_id'];
$patient->nurse_id = $_POST['nurse_id'];
$patient->created = date('Y-m-d H:i:s');

if($patient->create()){
    $patient_arr=array(
        "status" => true,
        "message" => "Criado com sucesso!",
        "id" => $patient->id,
        "name" => $patient->name,
        "phone" => $patient->phone,
        "gender" => $patient->gender,
        "health_condition" => $patient->health_condition
        "doctor_id" => $patient->doctor_id
        "nurse_id" => $patient->nurse_id
    );
}
else{
    $patient_arr=array(
        "status" => false,
        "message" => "Nome já existe!"
    );
}
print_r(json_encode($patient_arr));
?>

and the form

    <?php
session_start();
if(!isset($_SESSION['username'])){
   header("location:/hospital/login.php");
}
  $content = '<div class="row">
                <!-- left column -->
                <div class="col-md-12">
                  <!-- general form elements -->
                  <div class="box box-primary">
                    <div class="box-header with-border">
                      <h3 class="box-title">Adicionar Paciente</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form role="form">
                      <div class="box-body">
                        <div class="form-group">
                          <label for="exampleInputName1">Nome</label>
                          <input type="text" class="form-control" id="name" placeholder="Introduza o Nome">
                        </div>
                        <div class="form-group">
                          <label for="exampleInputName1">Contacto</label>
                          <input type="text" class="form-control" id="phone" placeholder="Introduza o Contacto">
                        </div>
                        <div class="form-group">
                            <label for="exampleInputName1">Género</label>
                            <div class="radio">
                                <label>
                                <input type="radio" name="gender" id="optionsRadios1" value="0" checked="">
                                Masculino
                                </label>
                            </div>
                            <div class="radio">
                                <label>
                                <input type="radio" name="gender" id="optionsRadios2" value="1">
                                Feminino
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                          <label for="exampleInputName1">Condi??o de Sáude</label>
                          <input type="text" class="form-control" id="health" placeholder="Introduza a condi??o de saúde">
                        </div>
                        <div class="form-group">
                          <label for="exampleInputDoctorId">DoctorID</label>
                          <input type="number" class="form-control" id="doctor_id" placeholder="Introduza o ID do médico">
                        </div>
                        <div class="form-group">
                          <label for="exampleInputNurseId">NurseID</label>
                          <input type="number" class="form-control" id="nurse_id" placeholder="Introduza o ID do enfermeiro">
                        </div>
                      </div>
                      <!-- /.box-body -->
                      <div class="box-footer">
                        <input type="button" class="btn btn-primary" onClick="AddPatient()" value="Criar"></input>
                      </div>
                    </form>
                  </div>
                  <!-- /.box -->
                </div>
              </div>';
  include('../master.php');
?>
<script>
  function AddPatient(){

        $.ajax(
        {
            type: "POST",
            url: '../api/patient/create.php',
            dataType: 'json',
            data: {
                name: $("#name").val(),
                phone: $("#phone").val(),
                gender: $("input[name='gender']:checked").val(),
                health_condition: $("#health_condition").val(),
                doctor_id: $("#doctor_id").val(),
                nurse_id: $("#nurse_id").val(),  
            },
            error: function (result) {
                alert(result.responseText);
                console.log()
            },
            success: function (result) {
                if (result['status'] == true) {
                    alert("Paciente adicionado com sucesso!");
                    window.location.href = '/hospital/admin/patient';
                }
                else {
                    alert(result['message']);
                }
            }
        });
    }
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...