Senthilkumar Posted December 3, 2022 Share Posted December 3, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315610-insert-query-with-text-and-file/ Share on other sites More sharing options...
requinix Posted December 3, 2022 Share Posted December 3, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315610-insert-query-with-text-and-file/#findComment-1603225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.