<?php 
ob_start();   
session_start();
$conn=mysqli_connect("localhost","crmsahavasco","sa123AWEsadfa");
if(!$conn)
{
die("could not connect to".mysqli_error());
}
else
{
mysqli_select_db($conn,"crmsahavasco_kanakveda");
}
 $result = mysqli_query($conn,"SELECT * FROM appointment_details WHERE username='".$_REQUEST['username']."' AND password='".$_REQUEST['password']."'");
	
    if(mysqli_num_rows($result) > 0) {
      $_SESSION['is_logged_in'] = 1;
	  }
	if(isset($_SESSION['is_logged_in'])) {  

function randomPassword() {
    $alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
$unique_id = randomPassword();

// Data to be inserted
$appointment = $_REQUEST['appointment'];
$form_date = $_POST['form_date'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$mobile_no = $_POST['mobile_no'];
$birth_date = $_POST['birth_date'];
$aadhar_no = $_POST['aadhar_no'];
$child_height_inch = $_POST['child_height_inch'];
$child_height_cm = $_POST['child_height_cm'];
$child_weight_kg = $_POST['child_weight_kg'];
$email = $_POST['email'];
$occupation = $_POST['occupation'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$chief_complaints = $_POST['chief_complaints'];

$father_ht_cm = $_POST['father_ht_cm'];
$mother_ht_cm = $_POST['mother_ht_cm'];
$siblings_ht_cm = $_POST['siblings_ht_cm'];

$qry9q = "select * from  signatures_data where used_by=''";
$re9q = mysqli_query($conn,$qry9q);
while($row=mysqli_fetch_array($re9q))
{  
$signature = $row['signatures'];
$sid = $row['sid'];
}


$querycust = "update signatures_data set used_by='$mobile_no'  where sid = '$sid'" ;
$resultcust=mysqli_query($conn,$querycust);



$past_history = isset($_POST['past_history']) ? implode(',', $_POST['past_history']) : '';

$past_history_Other = $_POST['past_history_Other'];
if (!empty($past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $past_history .= ',' . $past_history_Other;
} else {
    // If $past_history is empty, assign the new record directly
    $past_history = $past_history_Other;
}


$medical_report = isset($_POST['medical_report']) ? implode(',', $_POST['medical_report']) : '';

$father_past_history = isset($_POST['father_past_history']) ? implode(',', $_POST['father_past_history']) : '';

$father_past_history_other = $_POST['father_past_history_other'];
if (!empty($father_past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $father_past_history .= ',' . $father_past_history_other;
} else {
    // If $past_history is empty, assign the new record directly
    $father_past_history = $father_past_history;
}



$Gfather_past_history = isset($_POST['Gfather_past_history']) ? implode(',', $_POST['Gfather_past_history']) : '';

$Gfather_past_history_other = $_POST['Gfather_past_history_other'];
if (!empty($Gfather_past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $Gfather_past_history .= ',' . $Gfather_past_history_other;
} else {
    // If $past_history is empty, assign the new record directly
    $Gfather_past_history = $Gfather_past_history_other;
}



$GMfather_past_history = isset($_POST['GMfather_past_history']) ? implode(',', $_POST['GMfather_past_history']) : '';

$GMfather_past_history_other = $_POST['GMfather_past_history_other'];
if (!empty($GMfather_past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $GMfather_past_history .= ',' . $GMfather_past_history_other;
} else {
    // If $past_history is empty, assign the new record directly
    $GMfather_past_history = $GMfather_past_history_other;
}

$mother_past_history = isset($_POST['mother_past_history']) ? implode(',', $_POST['mother_past_history']) : '';
$mother_past_history_other = $_POST['mother_past_history_other'];
if (!empty($mother_past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $mother_past_history .= ',' . $mother_past_history_other;
} else {
    // If $past_history is empty, assign the new record directly
    $mother_past_history = $mother_past_history_other;
}




$Gmother_past_history = isset($_POST['Gmother_past_history']) ? implode(',', $_POST['Gmother_past_history']) : '';

$Gmother_past_history_other = $_POST['Gmother_past_history_other'];
if (!empty($Gmother_past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $Gmother_past_history .= ',' . $Gmother_past_history_other;
} else {
    // If $past_history is empty, assign the new record directly
    $Gmother_past_history = $Gmother_past_history_other;
}


$GMmother_past_history = isset($_POST['GMmother_past_history']) ? implode(',', $_POST['GMmother_past_history']) : '';

$GMmother_past_history_other = $_POST['GMmother_past_history_other'];
if (!empty($GMmother_past_history)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $GMmother_past_history .= ',' . $GMmother_past_history_other;
} else {
    // If $past_history is empty, assign the new record directly
    $GMmother_past_history = $GMmother_past_history_other;
}



$developmental_history = isset($_POST['developmental_history']) ? implode(',', $_POST['developmental_history']) : '';
$menses = isset($_POST['menses']) ? implode(',', $_POST['menses']) : '';
$leucorrhoea = isset($_POST['leucorrhoea']) ? implode(',', $_POST['leucorrhoea']) : '';
$addiction = isset($_POST['addiction']) ? implode(',', $_POST['addiction']) : '';
$water_thirst = isset($_POST['water_thirst']) ? implode(',', $_POST['water_thirst']) : '';

$desire = isset($_POST['desire']) ? implode(',', $_POST['desire']) : '';

$desire_other = $_POST['desire_other'];
if (!empty($desire)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $desire .= ',' . $desire_other;
} else {
    // If $past_history is empty, assign the new record directly
    $desire = $desire_other;
}


$aversion = isset($_POST['aversion']) ? implode(',', $_POST['aversion']) : '';

$aversion_other = $_POST['aversion_other'];
if (!empty($aversion)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $aversion .= ',' . $aversion_other;
} else {
    // If $past_history is empty, assign the new record directly
    $aversion = $aversion_other;
}
$appetite = isset($_POST['appetite']) ? implode(',', $_POST['appetite']) : '';
$stool = isset($_POST['stool']) ? implode(',', $_POST['stool']) : '';
$urine = isset($_POST['urine']) ? implode(',', $_POST['urine']) : '';
$sleep = isset($_POST['sleep']) ? implode(',', $_POST['sleep']) : '';
$perspiration = isset($_POST['perspiration']) ? implode(',', $_POST['perspiration']) : '';
$Rheatandcold = isset($_POST['Rheatandcold']) ? implode(',', $_POST['Rheatandcold']) : '';
$mind = isset($_POST['mind']) ? implode(',', $_POST['mind']) : '';

$mind_other = $_POST['mind_other'];
if (!empty($mind)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $mind .= ',' . $mind_other;
} else {
    // If $past_history is empty, assign the new record directly
    $mind = $mind_other;
}



$memory = isset($_POST['memory']) ? implode(',', $_POST['memory']) : '';

$memory_other = $_POST['memory_other'];
if (!empty($memory)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $memory .= ',' . $memory_other;
} else {
    // If $past_history is empty, assign the new record directly
    $memory = $memory_other;
}



$anxiety = isset($_POST['anxiety']) ? implode(',', $_POST['anxiety']) : '';

$anxiety_other = $_POST['anxiety_other'];
if (!empty($anxiety)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $anxiety .= ',' . $anxiety_other;
} else {
    // If $past_history is empty, assign the new record directly
    $anxiety = $anxiety_other;
}




$like_dislike = isset($_POST['like_dislike']) ? implode(',', $_POST['like_dislike']) : '';

$like_dislike_other = $_POST['like_dislike_other'];
if (!empty($like_dislike)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $like_dislike .= ',' . $like_dislike_other;
} else {
    // If $past_history is empty, assign the new record directly
    $like_dislike = $like_dislike_other;
}



$your_nature = isset($_POST['your_nature']) ? implode(',', $_POST['your_nature']) : '';

$your_nature_other = $_POST['your_nature_other'];
if (!empty($your_nature)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $your_nature .= ',' . $your_nature_other;
} else {
    // If $past_history is empty, assign the new record directly
    $your_nature = $your_nature_other;
}



$fear_of = isset($_POST['fear_of']) ? implode(',', $_POST['fear_of']) : '';

$fear_of_other = $_POST['fear_of_other'];
if (!empty($fear_of)) {
    // If $past_history is not empty, concatenate with a comma and the new record
    $fear_of .= ',' . $fear_of_other;
} else {
    // If $past_history is empty, assign the new record directly
    $fear_of = $fear_of_other;
}







$medical_report1=$_FILES["medical_report1"]["name"];
if($medical_report1!='')
{
$filename=$_FILES["medical_report1"]["name"];
$tmp=$_FILES["medical_report1"]["tmp_name"];
move_uploaded_file($tmp,"medical_report/".$filename=$_FILES["medical_report1"]["name"]);
}

$medical_report=$_FILES["medical_report"]["name"];
if($medical_report!='')
{
$filename1=$_FILES["medical_report"]["name"];
$tmp=$_FILES["medical_report"]["tmp_name"];
move_uploaded_file($tmp,"medical_report/".$filename1=$_FILES["medical_report"]["name"]);
}


$term_condition = $_POST['term_condition'];

// SQL query to insert data into the database
$sql = "INSERT INTO case_study (form_date, first_name, last_name, mobile_no, birth_date, aadhar_no, child_height_inch, child_height_cm, child_weight_kg, email, occupation, address1, address2, city, state, zipcode, chief_complaints, past_history, medical_report, father_past_history, Gfather_past_history, GMfather_past_history, mother_past_history, Gmother_past_history, GMmother_past_history, developmental_history, menses, leucorrhoea, addiction, water_thirst, desire, aversion, appetite, stool, urine, sleep, perspiration, Rheatandcold, mind, memory, anxiety, like_dislike, your_nature, fear_of, medical_report1, term_condition,siblings_ht_cm,mother_ht_cm,father_ht_cm,signature,appointment,unique_id) VALUES ('$form_date', '$first_name', '$last_name', '$mobile_no', '$birth_date', '$aadhar_no', '$child_height_inch', '$child_height_cm', '$child_weight_kg', '$email', '$occupation', '$address1', '$address2', '$city', '$state', '$zipcode', '$chief_complaints', '$past_history', '$filename1', '$father_past_history', '$Gfather_past_history', '$GMfather_past_history', '$mother_past_history', '$Gmother_past_history', '$GMmother_past_history', '$developmental_history', '$menses', '$leucorrhoea', '$addiction', '$water_thirst', '$desire', '$aversion', '$appetite', '$stool', '$urine', '$sleep', '$perspiration', '$Rheatandcold', '$mind', '$memory', '$anxiety', '$like_dislike', '$your_nature', '$fear_of', '$filename', '$term_condition','$siblings_ht_cm','$mother_ht_cm','$father_ht_cm','$signature','$appointment','$unique_id')";

if ($conn->query($sql) === TRUE) {
    "New record created successfully";
	
	$qry4z = "select * from case_study where  unique_id='$unique_id'";
$re4z= mysqli_query($conn,$qry4z);
while($row=mysqli_fetch_array($re4z))
{  
$case_id = $row['case_id'];
}

	
} else {
    "Error: " . $sql . "<br>" . $conn->error;
}


$queryapp="update  appointment_details set case_id = '$case_id'  where username  = '".$_REQUEST['username']."' and  password  = '".$_REQUEST['password']."' and  appointment_id = '".$_REQUEST['appointment']."'";
$resultapp=mysqli_query($conn,$queryapp);



//exit();
// Close connection
$conn->close();
}

//exit();

?><script>
javascript:window.location.href="thank_you.html";
</script>
