<?php

require('../settings/database.php');
$table_name = 'questions';
if (isset($_POST['submit'])) {

    $allow = array("jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", "PNG", "pdf", "PDF");
    //1st File
    if($_FILES['photo1']['name'] == "") {
        //echo "No Image"
    } else {
    
        $photo1=basename($_FILES['photo1']['name']);
        $extension = pathinfo($photo1, PATHINFO_EXTENSION);
        if(in_array($extension,$allow)){
            $target_path = "photo/";
            $photo1 = md5(rand() * time()).'.'.$extension;
            $target_path = $target_path . $photo1;
            move_uploaded_file($_FILES['photo1']['tmp_name'], $target_path);
            $sql = ($photo1!='')?" photo='$photo1' ". ',':'';
        }
    
    }

    $field1 = $_POST['field1'];
    $field2 = $_POST['field2'];
    $field3 = $_POST['field3'];
    $field4 = $_POST['field4'];
    $insert_bookings = "INSERT INTO `questions` SET
    $sql
       parent = '" . addslashes($field1) . "',
        child = '" . addslashes($field2) . "',
        age = '" . addslashes($field3) . "',
        problem = '" . addslashes($field4) . "',        
        status = 'Active'";

    $sql_insert = $dbconn->prepare($insert_bookings);
    $sql_insert->execute();
    $myid = $dbconn->lastInsertId();
    $message = "Details successfully updated.";
    // $status = "success";
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body style="text-align: center;">
    <form method="POST" enctype="multipart/form-data" >
        <select name="field1">
            <option value=" ">Select Parent</option>
            <option value="animal">Animal</option>
            <option value="fish">Fish</option>
            <option value="crops">Crops</option>
        </select><br><br>
        <select name="field2">
            <option value="">Select Child</option>
            <option value="dog">Cow</option>
            <option value="cat">Fish</option>
            <option value="cow">Crops</option>
            <option value="goat">Goat</option>
        </select><br><br>
        <label for="ageYears">Age (Years):</label><br>
        <input type="number" name="field3" value="0">Years<br>

        <label for="photo1">Photo 1:</label><br>
        <input type="file" name="photo1"><br><br>

        <textarea name="field4" id="editor" rows="4" cols="50" placeholder="Describe your problem here"></textarea><br><br>
        <button name="submit" type="submit">Submit</button>
    </form>
</body>

</html>