Reputation: 541
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.
Upvotes: 1
Views: 3905
Reputation: 258
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
}
}
}
}
Upvotes: 2
Reputation: 419
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.
Upvotes: 2