php - How to insert multiple rows and columns in database using array

Please i want to know if it is posible to use one sql query to insert into multiple columns and rows using array in php

Please i want to know if it is posible to use one sql query to insert into multiple columns and rows using array in php
$sql = "INSERT INTO MyGuests (firstname, lastname, email) <br> VALUES ('John', 'Doe', '[email protected]');";
but i want to represent column names in array as arrays and then values as array values, something that will look like this in php
$sql = "INSERT INTO myguest (array(firstname => 'john'));

Please i want to know if it is posible to use one sql query to insert into multiple columns and rows using array in php
$sql = "INSERT INTO MyGuests (firstname, lastname, email) <br> VALUES ('John', 'Doe', '[email protected]');";
but i want to represent column names in array as arrays and then values as array values, something that will look like this in php
$sql = "INSERT INTO myguest (array(firstname => 'john'));

Share Improve this question asked May 27, 2020 at 8:28 pandglobalpandglobal 431 silver badge9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

That is not an accepted SQL syntax per the MySQL manual. You must

INSERT INTO MyGuests
        (firstname, lastname,   email)
VALUES  ('John',    'Doe',      '[email protected]')

But, and this is an expansion well beyond the scope of your question, if you have an array of this information, you could iterate over the whole thing to build your SQL string.

// The array of information
$guests = array(
    array(
        'first_name'    => 'John',
        'last_name'     => 'Doe',
        'email'         => '[email protected]',
    ),
    array(
        'first_name'    => 'Jane',
        'last_name'     => 'Doe',
        'email'         => '[email protected]',
    ),
);
// Build the SQL string
$sql = '';
foreach($guests as $g) {
    if($sql != '') $sql.= ',';
    $sql .= '("'. $g['first_name'] .'", "'. $g['last_name'] .'", "'. $g['email'] .'")';
}
if($sql != '') {
    $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ". $sql;
}
// $sql now is either empty or full of lots of records to insert.

I used the description above to figure out a more better and simpler way of doing this without using arrays, even though you may want to use array, it still looks thesame way.

Below is the Code

$column = '(firstname, lastname, email)';  // here i can choose any column i wish
$value = "
    ('John', 'Doe', '[email protected]'),
    ('Jane', 'Doke', '[email protected]'),
    ('John3', 'Doe3', '[email protected]'),
    ('Jane4', 'Doke4', '[email protected]')
     ";


$sql = "INSERT INTO a_boy ". $column . " VALUES ". $value;  

This inserts a four row data into the database table called a_boy, so i can only keep extending the values to as many number of rows and details i wish to insert, and this makes my code simple and easy to use, since the goal was to insert many columns and many rows using one single query.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742397140a4436180.html

相关推荐

  • php - How to insert multiple rows and columns in database using array

    Please i want to know if it is posible to use one sql query to insert into multiple columns and rows using array in php

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信