My csv file looks like this:
Name | DateOfBirth| Gender| Occupation| DateOfTest | FieldNumber | Eye | ThresholdValue
Fred, 12/08/68,M, carpenter, 12/02/06, 1, R, 1234
Susan, 3/09/72, F, hairstylist, 12/04/07, 1, R, 1234
Fred, 12/08/68,M, carpenter, 12/02/06, 1, L, 1234
Susan, 3/09/72, F, hairstylist, 12/04/07, 1, L, 1234
Fred, 12/08/68,M, carpenter, 12/02/08, 1, R, 1234
Susan, 3/09/72, F, hairstylist, 12/04/08, 1, R, 1234
Fred, 12/08/68,M, carpenter, 12/02/08, 1, L, 1234
Susan, 3/09/72, F, hairstylist, 12/04/08, 1, L, 1234
Each patient will have two rows(for left and right eye) for each instance of test undertaken by them and they can undertake multiple tests. I have to put this information in a database in a way that it can be extracted from different dimensions like analyse data from all females, or analyse data from all carpenters etc. I thought of following two tables:
Patient_info
PatientID(auto-increment primary key), name, dob, gender, occupation
test_info
testID(auto-increment primary key), patientID(foreign key from patient_info), dot, fieldnumber, eye, thresholdvalue
I have been able to populate patient_info table by using fgetcsv() function of php. $sql works fine but $sql2 does not.
<?php
ini_set("auto_detect_line_endings", true);
include "connection.php";
$deleterecords = "TRUNCATE TABLE patient_info";
mysql_query($deleterecords);
$deleterecords = "TRUNCATE TABLE test_info";
mysql_query($deleterecords);
if(isset($_POST['submit']))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $fileop[0];
$dateofbirth = $fileop[1];
$gender = $fileop[2];
$occupation = $fileop[3];
$dateoftest = $fileop[4];
$field = $fileop[5];
$eye = $fileop[6];
$thresholdvalues = $fileop[7];
$sql = mysql_query("INSERT INTO patient_info (Name, DateOfBirth, Gender, Occupation) VALUES ('$name','$dateofbirth','$gender','$occupation')" );
$sql2 = mysql_query("INSERT INTO test_info (DateOfTest, FieldNumber, Eye, ThresholdValues) VALUES ('$dateoftest','$field','$eye','$thresholdvalues')" );
}
fclose($handle);
if($sql2) { echo 'test data successfully uploaded';}
if($sql)
{
echo 'Data uploaded successfully';
header("Location: http://localhost/lei/test.com/proper/upload_page.php");
exit();
}
else
{
echo 'Upload Failed';
}
}
?>
Questions: 1) How can I insert just one record for each patient in patient_info table, but multiple records for each instance of test undertaken by the patient in test_info table?? 2) How should I populate test_info table using patientID from patient_info table and other values from csv file? 3) I want to empty both the tables every time a new csv file is uploaded. I was using TRUNCATE query to do that, but since I defined PatientID as foreign key, TRUNCATE does not work. How can I empty both the tables before uploading a new csv file?
Thanks in advance !