Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
How to use multiple array in foreach
Array
(
    [0] => Array
        (
            [emp_name] => 
            [emp_title] => Senior Developer
            [emp_master_id] => 0
            [emp_designation] => TL
            [emp_skills] => PHP, MySQL
            [emp_experience] => 4-5
            [emp_location] => Chennai
        )

    [1] => Array
        (
            [emp_name] => 
            [emp_title] => Web Developer
            [emp_master_id] => 0
            [emp_designation] => Senior Developer
            [emp_skills] => 10Base-T Switching, PHP
            [emp_experience] => 4-5
            [emp_location] => Chennai
        )

)

I want Insert the array In Db by using Foreach.. Any Suggestions

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

let say your array name is $data

foreach($data as $key => $value){
  $sql="Insert into table_name (empname,..... )values('".$value['empname']."',....);";

  $result=$conn->query($sql);

}

This should do the work, Assuming that you have connection variable something like

$conn = new mysqli($servername, $username, $password, $dbname);

Give the column name as in your table in the sql query.

EDIT
The secure way of doing it

foreach($data as $key => $value){
// prepare and bind
$stmt = $conn->prepare("INSERT INTO table_name(emp_name, title, ...) VALUES (?, ?, ..)");//No of question marks are equal to columns you have mentioed
$stmt->bind_param("sss", $empName, $title, ...);
//   i - integer
//   d - double
//   s - string
//   b - BLOB


// set parameters and execute
$empName= $value['empname'];
$title= $value['title'];
//continue to do it for all...

$stmt->execute();

$stmt->close();

}
$conn->close();


NOTE: You can use function also if you know how to do it, less code inside the foreach
Happy coding :)

over 4 years ago · Santiago Trujillo Report

0

foreach($arr as $v){
    $sql = ') VALUES (';
    foreach($v as $k => $value){
        $sql = ",".$k.$sql.'"'.$value.'",';
    }
    $sql = 'Insert into table_name ('.trim($sql,',').';';
    $result=$conn->query($sql);
}

this maybe working.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!