Jump to content

Insert query with text and file


Senthilkumar

Recommended Posts

Dear Team,

I am new on this forum. 

I am trying to insert test and file from my GI. My code is 

 

 <?php

    if(isset($_POST['submit'])){

        $Engg_Name = $Name;
        $Engg_No = $Emp_No;
        $Month = $_POST['month'];


        if(!empty($_POST['lang'])) {
            foreach($_POST['lang'] as $ID => $VAL){

                $Point_Description = $_POST['description'] [$ID];
                $Point_Marks = $_POST['rating'] [$ID];
                $Marks_Target = $_POST['target'] [$ID];
                $Marks_Actual = $_POST['actual'][$ID];
                $Marks_Description = $_POST['remarks'] [$ID];
                $name = $_FILES['file']['name'] [$ID];
                $target_dir = "uploads/";
                $target_file = $target_dir . basename($_FILES["file"]["name"][$ID]);
                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

                if(move_uploaded_file($_FILES['file']['tmp_name'][$ID],$target_dir.$name)){
                    $insertquery = "INSERT INTO kra.marks (Engg_Name, Engg_No, Month, Point_Description, Point_Marks, Marks_Target, Marks_Actual, Marks_Description, Document) VALUES('$Engg_Name', '$Engg_No', '$Month', '$Point_Description', '$Point_Marks', '$Marks_Target', '$Marks_Actual', '$Marks_Description', '$name')";
                    $result = mysqli_query($conn, $insertquery);
                }

            }


        }

    }

    ?>
 <tr>
                                <td align='center' style="width:4%">
                                    <?php echo $n++; ?>
                                </td>
                                <td style="width:25%">
                                    <?php echo $Ass_Description; ?>
                                </td>
                                <td hidden align='center' style="width:5%">
                                    <input type="text" name="description[]" id="target" value="<?php echo $Ass_Description; ?>" class="tbl-input-cus" tabindex="1" />
                                </td>
                                <td style="width:25%">
                                    <?php echo $Ass_Marks; ?>
                                </td>
                                <td hidden align='center' style="width:5%">
                                    <input type="text" name="rating[]" id="target" value="<?php echo $Ass_Marks; ?>" class="tbl-input-cus" tabindex="1" />
                                </td>
                                <td align='center' style="width:5%">
                                    <input type="text" name="target[]" id="target" value="1" class="tbl-input-cus" tabindex="1" />
                                </td>
                                <td align='center' style="width:5%">
                                    <input type="text" name="actual[]" id="actual" value="" class="tbl-input-cus" tabindex="1" />
                                </td>
                                <td align='center' style="width:17%">
                                    <input type="text" name="remarks[]" id="remarks" value="" class="tbl-input-cus1" tabindex="1" />
                                </td>
                                <td align='center' style="width:19%">
                                    <input type="file" name="file[]" id="file" value="" class="tbl-input-cus1" tabindex="1" />
                                </td>
                                <td hidden align='center' style="width:10%">
                                    <input type="checkbox" checked="checked" class="checkbox" name='lang[]' value="<?php echo $ID; ?>" />
                                </td>
                            </tr>

In above code, If the file is not attaching values are not transferring to database. i want if file is not attached also text field should insert to database.

 

Can any one please help me to solve this

Link to comment
Share on other sites

Right now the code assumes there is a file.

$name = $_FILES['file']['name'] [$ID];
if(move_uploaded_file($_FILES['file']['tmp_name'][$ID],$target_dir.$name)){

What you need to change is have it first test if there was an uploaded file (the "error" in $_FILES is UPLOAD_ERR_OK). Then you only try to move the file if there was one.

Then have your insert query not depend on moving that file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.