* Add DB Rows * @param string table * @param array data * [ array($field => $value, ...) ] */
(
$table, array $data
)
| 473 | * [ array($field => $value, ...) ] |
| 474 | */ |
| 475 | public static function add_db_rows( |
| 476 | $table, array $data |
| 477 | ){ |
| 478 | |
| 479 | // Let's first format the field names. Note: |
| 480 | // these field names should be represented in the |
| 481 | // first set of rows as they are in the last. |
| 482 | $field_names = implode(', ',array_keys($data[0])); |
| 483 | |
| 484 | // Now we format the unprepared values. |
| 485 | $values = ''; |
| 486 | $value_count = 0; |
| 487 | foreach ($data as $datum){ |
| 488 | $value_count++; |
| 489 | $values.= '('.str_repeat('?, ', count( |
| 490 | $datum |
| 491 | )); |
| 492 | $values = substr($values, 0, -2); |
| 493 | $values.= ')'; |
| 494 | if($value_count !== count($data)) |
| 495 | $values.= ', '; |
| 496 | } |
| 497 | |
| 498 | // All the values need to go into their own params. |
| 499 | $value_count = 0; |
| 500 | $params = array(); |
| 501 | foreach ($data as $datum){ |
| 502 | foreach($datum as $value){ |
| 503 | if(is_array($value)){ |
| 504 | $value = serialize($value); |
| 505 | } |
| 506 | array_push($params, $value); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // Time to prepare the SQL! |
| 511 | $sql = 'INSERT INTO `'.$table.'` ('.$field_names |
| 512 | .') VALUES '.$values.';'; |
| 513 | |
| 514 | // Let's execute the query |
| 515 | $result = self::query($sql, $params, true); |
| 516 | |
| 517 | // And complete the query. |
| 518 | return $result; |
| 519 | |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Update DB Entry |
no test coverage detected