1

I am uploading multiple files with input box. I want to add that files in multiple rows with different id in mysql table.But its now working for me.

$paye_path=$obj->uploadMultiplePDF($_FILES['paye_inv'],$com_name); 

above code works fine uploadMultiplePDF function is working for me.files are goes into my folder.

After this i am calling my insert function:

$query  = $obj->insert_pay_slip_data($paye_path,$com_name);

it not working for me. this shows me below error:

Warning: Invalid argument supplied for foreach() in

Below is my insert query where i am using foreach loop:

public function insert_pay_slip_data($data,$com_name)
            {

                $con = $this->__construct();
                $shareArray = array();
                foreach($data as $shK=>$shVal)
                    {
                        $shareArray[$shK] = $shVal; 
                    } 
                 foreach ($shareArray as $keyshar => $valueshar) 
                 {  
                    $sql = "INSERT INTO `pay_slips`(`paye_id`, `trade_id`, `inv_pdf`, `created_date`, 
                    `created_by`) VALUES (LAST_INSERT_ID(), '".$com_name."','".$valueshar['paye_path']."',NOW(),'".$_SESSION['email']."')";
                    $execute = mysqli_query($con, $sql);
                    return $execute;
                }
            }

In inv_pdf column file name should be come. and if i upload 5 files then 5 different rows should be created in table. But its not working. I might be wrong in foreach loop.

3
  • 1
    You supplied an invalid argument to foreach. var_dump($data) can help you to know if $data is not empty Commented Jun 20, 2018 at 10:22
  • data is inserting now. but format is wrong. all files are going into same row with comma seperated. i want different rows Commented Jun 20, 2018 at 10:34
  • on print_r its shows data like this Array ( [0] => 1587701784152949112515934afa38db134c80f2444e07734a3f.pdf,21226947371529491125169512017.pdf ) Commented Jun 20, 2018 at 10:38

2 Answers 2

2

Before passing value to foreach you need to make sure type of value is Array

if(is_array($value)){
   foreach($value as $key => $val){
      // Your code
   }
}else{
   // Do nothing
}

To insert You can use this code

public function insert_pay_slip_data($data,$com_name){
   $con = $this->__construct();
   $shareArray = array();
   foreach($data as $key=>$value){
       $valueArray = explode(",",$value);
       foreach($valueArray as $index=>$path){
          $sql = "INSERT INTO `pay_slips`(`paye_id`, `trade_id`, `inv_pdf`, `created_date`, `created_by`) VALUES (LAST_INSERT_ID(), '".$com_name."','".$path."',NOW(),'".$_SESSION['email']."')";
          $response = mysqli_query($con, $sql);
          if(!$response){
             // Insertion Failed
          }
       }
   } 
}
Sign up to request clarification or add additional context in comments.

Comments

2

Do make some breakpoint on the line after you loop $data on foreach. You can var_dump it and see the structur, seeing this line $shareArray[$shK] = $shVal; I doubt your data is actually an array that could be used this way '".$valueshar['paye_path']."'

Maybe give some clarity on explanation by creating breakpoint on each line to make sure the data structure is correct. Also because the return statement inside the loop your function will stop prematurely if it need to process more than 1 child array.

6 Comments

how to change Array ( [0] => 2017.pdf,25021.pdf,6940178.pdf,) to Array ( [0] => 2017.pdf,[1] => 25021.pdf,[2] =>6940178.pdf,)
on var_dump $data getting this array(1) { [0]=> string(144) "2886815291529493312789dbc5e8bbc27c48b5892130a6650f7.pdf,485206316152949331215934afa38db134c80f2444e07734a3f.pdf,4805235611529493312169512017.pdf" }
to change Array ( [0] => 2017.pdf,25021.pdf,6940178.pdf,) to separated format you could use something like $data = explode(',', $data[0]) if you are perfectly sure that it will enter key 0.
I think you just need to format input data to needed format, so playing arround with the string will be enough. Please do read on explode(), 'implode()' to format array. After getting the proper array format you could just implode the formated arrray field when doing insert with 1 statement using implode. You could try something like $dataToBeInserted['paye_id'] = $paye_id; $dataToBeInserted['trade_id'] = $trade_id; and have php version of structured field ready to be inserted.
your explode is working great but in foreach loop only last file is there not all files are coming in foreach . and how to insert [0]=>file1 in first row, then [1]=> file2 in second row in db
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.