dongshi7350 2013-02-02 17:15
浏览 20
已采纳

将值插入多个表中

Okay, I am trying to submit values from a php form into multiple tables. My php code is working fine but values such as patientID are inserting into "patients" for example: PatientID; 100 fine but the same value for PatientID is not inserting the same unique value into another table for example: the "Disease" table. Am I doing something wrong?

**revised question

I'm not sure if I have the relationships between the tables correctly assigned. Here are the tables and the relationships between them.

Patient Attends Accident & Emergency 
Patient seen_by Nurse
Nurse assesses disease of patient 
{{nurse assigns priority to patient}} Priority linked to patient and nurse
{{nurse gives patient waiting time}} Time linked to nurse and patient 
{{doctor will see patient based on their waiting time and priority}} Doctor linked to both time and priority. 
Accident & Emergency; (ID(PK), PatientID(FK) Address, City, Postcode, Telephone)
Patient (ID(PK), Forename, Surname, Gender, Dateofbirth, Address, Patienthistory, illness, 
Nurse(ID(PK) Forename, surname)
Assesses(ID(PK)NurseID(FK), PatientID(FK))
Disease(ID(PK), illness, symptoms, diagnosis, treatment) {{nurse assesses disease of patient (these tables should all be linked}}
Priority (ID, NurseID(FK), PatientID(FK), DoctorID(FK), Priority)
Time(ID,NurseID, PatientID, DoctorID, Arrival Time, Expected waiting time, Discharge time)
Doctor (ID,Firstname, Surname)

Revised PHP code. ID is not inserting into tables; for example: PatientID is not inserting into the Disease table.

<?php
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');

//get NURSE values from form
$nurse_ID = $_POST['nurse_ID'];
$nurse_name = $_POST['nurse_name'];
$nurse_lastname = $_POST['nurse_lastname'];

//get Disease values from form
$disease_ID = $_POST['disease_ID'];
$symptoms = $_POST['symptoms'];
 $diagnosis = $_POST['diagnosis'];
$treatment = $_POST['treatment'];

//get Patient values from form 
$patient_id = $_POST['patient_id'];
$patient_name = $_POST['patient_name'];
$patient_lastname = $_POST['patient_lastname'];
$gender = $_POST['gender'];
 $dateOfBirth = $_POST['dateOfBirth'];
$monthOfBirth = $_POST['monthOfBirth'];
$yearOfBirth = $_POST['yearOfBirth'];
$address = $_POST['address'];
$history = $_POST['history'];
$illness = $_POST['illness'];
$priority = $_POST['priority'];
$priority_id = $_POST['priority_id'];

// Validate
$date = $dateOfBirth.'-'.$monthOfBirth.'-'.$yearOfBirth;

$sql ="INSERT INTO Nurse(Forename, Surname)
VALUES('$nurse_name', '$nurse_lastname')";
mysql_query($sql,$con) or die('Error: ' . mysql_error());
echo "$nurse_ID"; mysql_insert_id(); //get the assigned id for a nurse

$sql ="INSERT INTO Disease(Illness, Symptoms, Diagnosis, Treatment, PatientID)
   VALUES('$illness', '$symptoms', '$diagnosis', '$treatment', '$patient_id')";
mysql_query($sql,$con) or die('Error: ' . mysql_error());
echo "$patient_id"; mysql_insert_id(); //get the assigned id for a patient 

//use nurse_id and patient_id
$sql ="INSERT INTO Priority(NurseID, PatientID, Priority)
   VALUES('$nurse_ID', '$patient_id', '$priority')";
mysql_query($sql,$con) or die('Error: ' . mysql_error());
echo "$priority_id"; mysql_insert_id(); //get the assigned id for priority
echo "$patient_id"; mysql_insert_id(); //get the assigned id for a patient

$sql="INSERT INTO Patient(Forename, Surname, Gender, Date_Of_Birth, Address, Patient_History, Illness, Priority)
  VALUES     ('$patient_name', '$patient_lastname', '$gender', '$date', '$address', '$history', '$illness', '$priority')";
 mysql_query($sql,$con) or die('Error: ' . mysql_error());
 echo "$patient_id"; mysql_insert_id(); //get the assigned id for a patient

echo "1 record added";
 // close connection 
 mysql_close($con);
 ?>
  • 写回答

2条回答 默认 最新

  • doufei16736 2013-02-02 18:09
    关注
    1. you need to use unique ids, names and lastname for different entities (nurse, patient, disease etc). And then use them appropriately in INSERT statements. See revised code below.
    2. select your db only once at the beginning of the script with mysql_select_db (if you planning to stick with mysql_*).
    3. Sanitize and validate input from the user before inserting it.
    4. Insert your records in correct (logical) order (nurse, patient, disease, priority).
    5. Now all of your ids come via POST. You might consider using id auto-reneration in mysql.
    6. You have a missing variable $priority_id. I've put it in the revised code assuming that you get it the same way via POST.
    7. Do proper error handling not just die().
    8. Better consider to switch to PDO or mysqli_* and use prepared statements.

    Revised code (updated):

    Assumption is that auto_increment is enabled for the id column of every table.

    $con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
    mysql_select_db("a&e", $con) or or die('Could not select database.');
    
    //get NURSE values from form
    //We don't need to post an id for a Nurse since mysql will assign it for us
    //$nurse_id = $_POST['nurse_id'];
    $nurse_name = $_POST['nurse_name']; 
    $nurse_lastname = $_POST['nurse_lastname'];
    
    //get Disease values from form
    // We don't need to post an id for a Disease since mysql will assign it for us
    //$disease_id = $_POST['disease_id'];
    $symptoms = $_POST['symptoms'];
    $diagnosis = $_POST['diagnosis'];
    $treatment = $_POST['treatment'];
    
    //get Patient values from form
    //We don't need to post an id for a Patient since mysql will assign it for us
    //$patient_id = $_POST['patient_id'];
    $patient_name = $_POST['patient_name'];
    $patient_lastname = $_POST['patient_lastname'];
    $gender = $_POST['gender'];
    $dateOfBirth = $_POST['dateOfBirth'];
    $monthOfBirth = $_POST['monthOfBirth'];
    $yearOfBirth = $_POST['yearOfBirth'];
    $address = $_POST['address'];
    $history = $_POST['history'];
    $illness = $_POST['illness'];
    $priority = $_POST['priority'];
    
    //We don't need to post an id for a Priority entity since mysql will assign it for us
    //missing variable
    //$priority_id = $_POST['priority_id'];
    
    //Sanitize and validate your input here 
    // ...skipped
    // Validate
    $date = $dateOfBirth.'-'.$monthOfBirth.'-'.$yearOfBirth;
    
    //We don't provide an id for a Nurse since mysql will assign it for us
    $sql ="INSERT INTO Nurse(Forename, Surname)
           VALUES('$nurse_name', '$nurse_lastname')";
    mysql_query($sql,$con) or die('Error: ' . mysql_error());
    $nurse_id = mysql_insert_id(); //get the assigned id for a nurse
    
    //We don't provide an id for a Patient since mysql will assign it for us
    $sql="INSERT INTO Patient(Forename, Surname, Gender, Date_Of_Birth, Address, Patient_History, Illness, Priority)
          VALUES('$patient_name', '$patient_lastname', '$gender', '$date', '$address', '$history', '$illness', '$priority')";
    mysql_query($sql,$con) or die('Error: ' . mysql_error());
    $patient_id = mysql_insert_id(); //get the assigned id for a patient
    
    //We don't provide an id for a Disease since mysql will assign it for us
    $sql ="INSERT INTO Disease(Illness, Symptoms, Diagnosis, Treatment, PatientID)
           VALUES('$illness', '$symptoms', '$diagnosis', '$treatment', '$patient_id')";
    mysql_query($sql,$con) or die('Error: ' . mysql_error());
    
    //We don't provide an id for a Priority since mysql will assign it for us
    //But we use $nurse_id and $patient_id that we get earlier
    $sql ="INSERT INTO Priority(NurseID, PatientID, Priority)
           VALUES('$nurse_id', '$patient_id', '$priority')";
    mysql_query($sql,$con) or die('Error: ' . mysql_error());
    
    echo "1 record added";
    // close connection 
    mysql_close($con);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。