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

244
Views
Cómo usar matriz múltiple en 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 ) )

Quiero Insertar la matriz en Db usando Foreach ... Cualquier sugerencia

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

digamos que el nombre de su matriz es $datos

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

Esto debería hacer el trabajo, suponiendo que tiene una variable de conexión algo así como

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

Proporcione el nombre de la columna como en su tabla en la consulta sql .

EDITAR
La forma segura de hacerlo

 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();


NOTA: También puede usar la función si sabe cómo hacerlo, menos código dentro del foreach
Feliz codificación :)

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); }

esto tal vez funcione.

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!