Joining array values into string in MySQL multiple insert

Saito Gurung

In a form, multiple Checkbox values to be inserted into database:

My Code:

Array: ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

$a = $_POST['id']; // data from form
$query = "INSERT INTO abc(`x`,`y`,`z`) VALUES " . implode (",","(NULL,$a,'1')");

mysqli_query($dbc,$query);

There seems to be a problem with implode function. How do you concat array using implode?

// Expected output

INSERT INTO abc(`x`,`y`,`z`) VALUES (NULL,1,'1'),(NULL,2,'1'),(NULL,3,'1'),(NULL,4,'1'),

Column y of table abc needs to loop with $a.

Kevin

If you want to create a batch multiple insertions, first build the batches first, then implode those batches:

$multiple = array_map(function($e) use($dbc) {
    $e = $dbc->real_escape_string($e);
    return "(NULL, $e, '1')";
}, $a);
$query = "INSERT INTO abc(`x`,`y`,`z`) VALUES " . implode (',', $multiple);
mysqli_query($dbc,$query);

Sidenotes: Its not VALUE, Its VALUES. And remember to use the correct quotes on identifiers. Its supposed to be backticks not single quotes.

INSERT INTO abc('x','y','z') // NOT OK
INSERT INTO abc(`x`,`y`,`z`) // OK

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Joining array values into string in MySQL multiple insert

From Dev

MySQL - INSERT multiple values conditionally

From Dev

MySQL - INSERT multiple values conditionally

From Dev

Ruby: Insert Multiple Values Into String

From Dev

MySQL insert array of hardcoded values

From Dev

Insert array values into mysql database

From Dev

PHP insert the array values into mysql

From Dev

Joining multiple tables in MySQL

From Dev

Joining multiple tables in MYSQL

From Dev

Joining multiple tables in MySQL

From Dev

Array - Insert array values to mysql table

From Dev

how to insert multiple of values in a single field in mysql

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

Insert multiple values in one column of mysql database

From Dev

Insert values from multiple arrays into MySQL

From Dev

Insert multiple values from API into mysql database

From Dev

Creating a single insert statement for multiple array values

From Dev

how to insert array values into mysql with the same id

From Dev

PHP/MySQL Insert distinct array values

From Dev

How to insert data as array values into MySQL?

From Dev

Joining property values of objects in an array

From Dev

Flatten array joining keys with values

From Dev

Different values on joining array in Javascript

From Dev

Joining and combining multiple tables MYSQL

From Dev

Multiple INSERT INTO MySQL from $_POST array

From Dev

insert multiple rows via a php array into mysql

From Dev

Insert different values in array in MySQL in the same columns with just 1 insert

From Dev

PHP Insert an array as a string into mysql text field

From Dev

How to insert values inside array of array elements in json using json ?Or How to insert values in this below json string

Related Related

  1. 1

    Joining array values into string in MySQL multiple insert

  2. 2

    MySQL - INSERT multiple values conditionally

  3. 3

    MySQL - INSERT multiple values conditionally

  4. 4

    Ruby: Insert Multiple Values Into String

  5. 5

    MySQL insert array of hardcoded values

  6. 6

    Insert array values into mysql database

  7. 7

    PHP insert the array values into mysql

  8. 8

    Joining multiple tables in MySQL

  9. 9

    Joining multiple tables in MYSQL

  10. 10

    Joining multiple tables in MySQL

  11. 11

    Array - Insert array values to mysql table

  12. 12

    how to insert multiple of values in a single field in mysql

  13. 13

    PDO Insert multiple checkbox values in Mysql

  14. 14

    Insert multiple values in one column of mysql database

  15. 15

    Insert values from multiple arrays into MySQL

  16. 16

    Insert multiple values from API into mysql database

  17. 17

    Creating a single insert statement for multiple array values

  18. 18

    how to insert array values into mysql with the same id

  19. 19

    PHP/MySQL Insert distinct array values

  20. 20

    How to insert data as array values into MySQL?

  21. 21

    Joining property values of objects in an array

  22. 22

    Flatten array joining keys with values

  23. 23

    Different values on joining array in Javascript

  24. 24

    Joining and combining multiple tables MYSQL

  25. 25

    Multiple INSERT INTO MySQL from $_POST array

  26. 26

    insert multiple rows via a php array into mysql

  27. 27

    Insert different values in array in MySQL in the same columns with just 1 insert

  28. 28

    PHP Insert an array as a string into mysql text field

  29. 29

    How to insert values inside array of array elements in json using json ?Or How to insert values in this below json string

HotTag

Archive