skip to Main Content

I have a form with multiple fields to upload data and fetch the member’s data to the front end. In the front end, some sections have multiple points so I have created separate tables for them. To fetch data from different tables we need to create a foreign key and for this, I have made the member name a foreign key. In this, I have a problem if the backend user uploads data then he should not upload the same member name, for this, I need to make a foreign key a numeric number, but a numeric number foreign key is also a primary key so that it becomes auto_increment. But in tables like Qualification, Career, Achievement, and Issue, the member’s data will get separated because of auto_increment. So is there any way to make the foreign key numeric so that the foreign key will not get separated from the tables like Qualification, Career, Achievement, and Issue?

The following scripts are for handling the form-

<?php
    include "../partials/dbconnect.php"
?>

<?php
    if ($_SERVER['REQUEST_METHOD']=='POST') {
        $upload_name = $_POST['name'];
        $upload_pob = $_POST['pob'];
        $upload_dob = $_POST['dob'];
        $upload_father = $_POST['father'];
        $upload_mother = $_POST['mother'];
        date_default_timezone_set('Asia/Kolkata');
        $date = date('Y-m-d H:i:s');

        // File upload handling
        if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
            $fileTmpPath = $_FILES['image']['tmp_name'];
            $fileName = $_FILES['image']['name'];
            $fileNameCmps = explode(".", $fileName);
            $fileExtension = strtolower(end($fileNameCmps));

            // Allowed file extensions
            $allowedfileExtensions = array('jpg', 'jpeg', 'png', 'avif', 'webp');

            if (in_array($fileExtension, $allowedfileExtensions)) {
                // Directory in which the file will be saved
                $uploadFileDir = '../member_images/';
                $dest_path = $uploadFileDir.$fileName;

                if (move_uploaded_file($fileTmpPath, $dest_path)) {
                    $sql_member_profile = "INSERT INTO `member_profile` (`member_image`, `member_name`, `birth_place`, `birthday`, `father`, `mother`) VALUES ('$fileName', '$upload_name', '$upload_pob', '$upload_dob', '$upload_father', '$upload_mother')";
                    $result_member_profile = mysqli_query($conn, $sql_member_profile);

                    if ($result_member_profile) {
                        echo 'Image upload successfully';
                    }

                    
                    // start
                    // Qualification start
                    // Get the qualification data
                    $quas = $_POST['qua'];

                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlQua = "INSERT INTO member_qualifications (member_name, member_qualification, date_upload) VALUES (?, ?, ?)";
                    $stmt1 = $conn->prepare($sqlQua);

                    // Insert each qualification and date into the table
                    for ($i = 0; $i < count($quas); $i++) {
                        $stmt1->bind_param("sss", $upload_name, $quas[$i], $date);
                        $stmt1->execute();
                    }
                    // Qualification end


                    // Career start
                    // Get the career data
                    $cars = $_POST['car'];
                    $dates = $_POST['date'];

                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlCar = "INSERT INTO member_careers (member_name, member_career_year, member_career_context, date_upload) VALUES (?, ?, ?, ?)";
                    $stmt2 = $conn->prepare($sqlCar);

                    // Insert each qualification and date into the table
                    for ($i = 0; $i < count($cars); $i++) {
                        $stmt2->bind_param("ssss", $upload_name, $dates[$i], $cars[$i], $date);
                        $stmt2->execute();
                    }
                    // Career end


                    // Achievements start
                    // Get the achievement data
                    $achis = $_POST['achi'];

                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlAchi = "INSERT INTO member_achievements (member_name, member_achievement, date_upload) VALUES (?, ?, ?)";
                    $stmt3 = $conn->prepare($sqlAchi);

                    // Insert each achievement and date into the table
                    for ($i = 0; $i < count($achis); $i++) {
                        $stmt3->bind_param("sss", $upload_name, $achis[$i], $date);
                        $stmt3->execute();
                    }
                    // Achievements end


                    // Issues start
                    // Get the issue data
                    $isss = $_POST['iss'];

                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlIss = "INSERT INTO member_issues (member_name, member_issue, date_upload) VALUES (?, ?, ?)";
                    $stmt4 = $conn->prepare($sqlIss);

                    // Insert each issue and date into the table
                    for ($i = 0; $i < count($isss); $i++) {
                        $stmt4->bind_param("sss", $upload_name, $isss[$i], $date);
                        $stmt4->execute();
                    }
                    // Issues end


                    // Close the statement and connection
                    $stmt1->close();
                    $stmt2->close();
                    $stmt3->close();
                    $stmt4->close();


                    echo "File is successfully uploaded.";
                }else {
                    echo "There was some error moving the file to upload directory.";
                }
            }else {
                echo "Upload failed. Allowed file types: " . implode(',', $allowedfileExtensions);
            }
        }else {
            if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_NO_FILE) {
                $sql_member_profile = "INSERT INTO `member_profile` (`member_image`, `member_name`, `birth_place`, `birthday`, `father`, `mother`) VALUES ('user.png', '$upload_name', '$upload_pob', '$upload_dob', '$upload_father', '$upload_mother')";
                $result_member_profile = mysqli_query($conn, $sql_member_profile);

                if ($result_member_profile) {
                    echo 'Image upload successfully';
                }


                // start
                // Qualification start
                // Get the qualification data
                $quas = $_POST['qua'];

                // Prepare SQL statement to insert data into table "member_telephone"
                $sqlQua = "INSERT INTO member_qualifications (member_name, member_qualification, date_upload) VALUES (?, ?, ?)";
                $stmt1 = $conn->prepare($sqlQua);

                // Insert each qualification and date into the table
                for ($i = 0; $i < count($quas); $i++) {
                    $stmt1->bind_param("sss", $upload_name, $quas[$i], $date);
                    $stmt1->execute();
                }
                // Qualification end


                // Career start
                // Get the career data
                $cars = $_POST['car'];
                $dates = $_POST['date'];

                // Prepare SQL statement to insert data into table "member_telephone"
                $sqlCar = "INSERT INTO member_careers (member_name, member_career_year, member_career_context, date_upload) VALUES (?, ?, ?, ?)";
                $stmt2 = $conn->prepare($sqlCar);

                // Insert each qualification and date into the table
                for ($i = 0; $i < count($cars); $i++) {
                    $stmt2->bind_param("ssss", $upload_name, $dates[$i], $cars[$i], $date);
                    $stmt2->execute();
                }
                // Career end


                // Achievements start
                // Get the achievement data
                $achis = $_POST['achi'];

                // Prepare SQL statement to insert data into table "member_telephone"
                $sqlAchi = "INSERT INTO member_achievements (member_name, member_achievement, date_upload) VALUES (?, ?, ?)";
                $stmt3 = $conn->prepare($sqlAchi);

                // Insert each achievement and date into the table
                for ($i = 0; $i < count($achis); $i++) {
                    $stmt3->bind_param("sss", $upload_name, $achis[$i], $date);
                    $stmt3->execute();
                    }
                // Achievements end


                // Issues start
                // Get the issue data
                $isss = $_POST['iss'];

                // Prepare SQL statement to insert data into table "member_telephone"
                $sqlIss = "INSERT INTO member_issues (member_name, member_issue, date_upload) VALUES (?, ?, ?)";
                $stmt4 = $conn->prepare($sqlIss);

                // Insert each issue and date into the table
                for ($i = 0; $i < count($isss); $i++) {
                    $stmt4->bind_param("sss", $upload_name, $isss[$i], $date);
                    $stmt4->execute();
                }
                // Issues end


                // Close the statement and connection
                $stmt1->close();
                $stmt2->close();
                $stmt3->close();
                $stmt4->close();

                
                echo "File is successfully uploaded.";
            }else {
                echo "There was an error with the file upload. Error code: " . $_FILES['image']['error'];            
            }
        }
    }
?>

Following are the Tables-
Profile

Achievement

Career

Issues

Qualification

2

Answers


  1. Chosen as BEST ANSWER

    I have found the answer by suggesting @Adyson. With the help of the demo, I have found that I have to put mysqli_insert_id($conn) in just below the member_profile inserting the query.

    Following are my final codes that work for my application-

    <?php
        include "../partials/dbconnect.php"
    ?>
    
    <?php
        if ($_SERVER['REQUEST_METHOD']=='POST') {
            $upload_name = $_POST['name'];
            $upload_pob = $_POST['pob'];
            $upload_dob = $_POST['dob'];
            $upload_father = $_POST['father'];
            $upload_mother = $_POST['mother'];
            date_default_timezone_set('Asia/Kolkata');
            $date = date('Y-m-d H:i:s');
    
            // File upload handling
            if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
                $fileTmpPath = $_FILES['image']['tmp_name'];
                $fileName = $_FILES['image']['name'];
                $fileNameCmps = explode(".", $fileName);
                $fileExtension = strtolower(end($fileNameCmps));
    
                // Allowed file extensions
                $allowedfileExtensions = array('jpg', 'jpeg', 'png', 'avif', 'webp');
    
                if (in_array($fileExtension, $allowedfileExtensions)) {
                    // Directory in which the file will be saved
                    $uploadFileDir = '../member_images/';
                    $dest_path = $uploadFileDir.$fileName;
    
                    if (move_uploaded_file($fileTmpPath, $dest_path)) {
                        $sql_member_profile = "INSERT INTO `member_profile` (`member_image`, `member_name`, `birth_place`, `birthday`, `father`, `mother`) VALUES ('$fileName', '$upload_name', '$upload_pob', '$upload_dob', '$upload_father', '$upload_mother')";
                        $result_member_profile = mysqli_query($conn, $sql_member_profile);
    
                        $mid = mysqli_insert_id($conn);
    
                        if ($result_member_profile) {
                            echo 'Image upload successfully';
                        }
    
                        
                        // start
                        // Qualification start
                        // Get the qualification data
                        $quas = $_POST['qua'];
    
                        // Prepare SQL statement to insert data into table "member_telephone"
                        $sqlQua = "INSERT INTO member_qualifications (member_name, member_qualification, date_upload) VALUES (?, ?, ?)";
                        $stmt1 = $conn->prepare($sqlQua);
    
                        // Insert each qualification and date into the table
                        for ($i = 0; $i < count($quas); $i++) {
                            $stmt1->bind_param("sss", $upload_name, $quas[$i], $date);
                            $stmt1->execute();
                        }
                        // Qualification end
    
    
                        // Career start
                        // Get the career data
                        $cars = $_POST['car'];
                        $dates = $_POST['date'];
    
                        // Prepare SQL statement to insert data into table "member_telephone"
                        $sqlCar = "INSERT INTO member_careers (member_name, member_career_year, member_career_context, date_upload) VALUES (?, ?, ?, ?)";
                        $stmt2 = $conn->prepare($sqlCar);
    
                        // Insert each qualification and date into the table
                        for ($i = 0; $i < count($cars); $i++) {
                            $stmt2->bind_param("ssss", $upload_name, $dates[$i], $cars[$i], $date);
                            $stmt2->execute();
                        }
                        // Career end
    
    
                        // Achievements start
                        // Get the achievement data
                        $achis = $_POST['achi'];
    
                        // Prepare SQL statement to insert data into table "member_telephone"
                        $sqlAchi = "INSERT INTO member_achievements (member_name, member_achievement, date_upload) VALUES (?, ?, ?)";
                        $stmt3 = $conn->prepare($sqlAchi);
    
                        // Insert each achievement and date into the table
                        for ($i = 0; $i < count($achis); $i++) {
                            $stmt3->bind_param("sss", $upload_name, $achis[$i], $date);
                            $stmt3->execute();
                        }
                        // Achievements end
    
    
                        // Issues start
                        // Get the issue data
                        $isss = $_POST['iss'];
    
                        // Prepare SQL statement to insert data into table "member_telephone"
                        $sqlIss = "INSERT INTO member_issues (member_name, member_issue, date_upload) VALUES (?, ?, ?)";
                        $stmt4 = $conn->prepare($sqlIss);
    
                        // Insert each issue and date into the table
                        for ($i = 0; $i < count($isss); $i++) {
                            $stmt4->bind_param("sss", $upload_name, $isss[$i], $date);
                            $stmt4->execute();
                        }
                        // Issues end
    
    
                        // Close the statement and connection
                        $stmt1->close();
                        $stmt2->close();
                        $stmt3->close();
                        $stmt4->close();
    
    
                        echo "File is successfully uploaded.";
                    }else {
                        echo "There was some error moving the file to upload directory.";
                    }
                }else {
                    echo "Upload failed. Allowed file types: " . implode(',', $allowedfileExtensions);
                }
            }else {
                if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_NO_FILE) {
                    $sql_member_profile = "INSERT INTO `member_profile` (`member_image`, `member_name`, `birth_place`, `birthday`, `father`, `mother`) VALUES ('user.png', '$upload_name', '$upload_pob', '$upload_dob', '$upload_father', '$upload_mother')";
                    $result_member_profile = mysqli_query($conn, $sql_member_profile);
    
                    $noImage_mid = mysqli_insert_id($conn);
    
                    if ($result_member_profile) {
                        echo 'Image upload successfully';
                    }
    
    
                    // start
                    // Qualification start
                    // Get the qualification data
                    $quas = $_POST['qua'];
    
                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlQua = "INSERT INTO member_qualifications (member_name, member_qualification, date_upload) VALUES (?, ?, ?)";
                    $stmt1 = $conn->prepare($sqlQua);
    
                    // Insert each qualification and date into the table
                    for ($i = 0; $i < count($quas); $i++) {
                        $stmt1->bind_param("sss", $upload_name, $quas[$i], $date);
                        $stmt1->execute();
                    }
                    // Qualification end
    
    
                    // Career start
                    // Get the career data
                    $cars = $_POST['car'];
                    $dates = $_POST['date'];
    
                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlCar = "INSERT INTO member_careers (member_name, member_career_year, member_career_context, date_upload) VALUES (?, ?, ?, ?)";
                    $stmt2 = $conn->prepare($sqlCar);
    
                    // Insert each qualification and date into the table
                    for ($i = 0; $i < count($cars); $i++) {
                        $stmt2->bind_param("ssss", $upload_name, $dates[$i], $cars[$i], $date);
                        $stmt2->execute();
                    }
                    // Career end
    
    
                    // Achievements start
                    // Get the achievement data
                    $achis = $_POST['achi'];
    
                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlAchi = "INSERT INTO member_achievements (member_name, member_achievement, date_upload) VALUES (?, ?, ?)";
                    $stmt3 = $conn->prepare($sqlAchi);
    
                    // Insert each achievement and date into the table
                    for ($i = 0; $i < count($achis); $i++) {
                        $stmt3->bind_param("sss", $upload_name, $achis[$i], $date);
                        $stmt3->execute();
                        }
                    // Achievements end
    
    
                    // Issues start
                    // Get the issue data
                    $isss = $_POST['iss'];
    
                    // Prepare SQL statement to insert data into table "member_telephone"
                    $sqlIss = "INSERT INTO member_issues (member_name, member_issue, date_upload) VALUES (?, ?, ?)";
                    $stmt4 = $conn->prepare($sqlIss);
    
                    // Insert each issue and date into the table
                    for ($i = 0; $i < count($isss); $i++) {
                        $stmt4->bind_param("sss", $upload_name, $isss[$i], $date);
                        $stmt4->execute();
                    }
                    // Issues end
    
    
                    // Close the statement and connection
                    $stmt1->close();
                    $stmt2->close();
                    $stmt3->close();
                    $stmt4->close();
    
                    
                    echo "File is successfully uploaded.";
                }else {
                    echo "There was an error with the file upload. Error code: " . $_FILES['image']['error'];            
                }
            }
        }
    ?>
    

  2. You can change the approach like this –

    1. Create Qualifications, Achievements, and Issues tables which contains a profile_id field that acts as a foreign key referencing the id field in the Profiles table.
    2. This profile_id foreign key will have a one-to-many relationship, as each profile can have multiple qualifications, achievements, and issues.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search