How to use two foreach to insert data in database

how to insert multiple rows in mysql using php using foreach
two conditions in foreach php
php using a for loop to do a multi row insert into mysql db
how to insert multiple array values into database php
how to insert multiple json data into mysql database in php
how to insert array data into mysql database using php
mysql foreach insert
how to insert multiple values in a single column in mysql using php

I have two arrays from my view.I need to insert their data corresponding to each other.

Below is the My view

<?php foreach ($seats as $seat):?>

        <tr>
            <td><input type="checkbox" name="seat_id[]" value=<?php echo $seat->seatNumber;?>></td>
            <td><?php echo $seat->seatLabel;?></td>
            <td><input type="hidden" name="seat_label[]" value="<?php echo $seat->seatLabel;?>"></td>
        </tr>
        <?php endforeach;?>

From above view seat_id and seat_label carries values that have to be stored in database

This is my controller

if($this->form_validation->run()){
            $seat_ids = $this->input->post("seat_id[]");
            $seat_labels = $this->input->post("seat_label[]");
            $busNumber = $this->input->post("busNumber");
            $bookingDate = $this->input->post("bookingDate");
            $reportingTime = $this->input->post("reportingTime");
            $departureTime = $this->input->post("departureTime");
            $this->load->model('Queries');
     $insert = $this->Queries->saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime);

This is My model

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime){

    foreach($seat_ids as $seat_id )
          foreach($seat_labels as $seat_label ){
    {
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
    }

Hope this will help you :

Use insert_batch instead of insert like this :

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime)
{

    foreach($seat_ids as $key => $seat_id )
    {
            $record[] = array(
            'seatNumber' => $seat_id, 
            'seatLabel'  => $seat_labels[$key],
            'bookingDate' => $bookingDate,
            'reportingTime' => $reportingTime,
            'departureTime' => $departureTime, 
            'busNumber' => $busNumber,
            'seatUse' => 'Enabled',
            'seatStatus' => 'Available');
    }
    $this->db->insert_batch('schedule', $record);
}

For more :https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

How to use two foreach to insert data in database, Hope this will help you : Use insert_batch instead of insert like this : public function saveMessage($seat_ids, $seat_labels, $busNumber,  My database has a table called tblprojects with column names say, project_num, project_status, project_name. I want to use foreach loop to get the data from the database and display the records in php table. However I am unable to display the records with following code. Please help me in correcting it. Am new to using PHP.


your code should read

 foreach($seat_ids as $seat_id ){
     foreach($seat_labels as $seat_label ){
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
   }
}

Insert Multiple Rows Into MySQL The Right Way With PHP, The data may come from a feed, an array in yousr code, a form submitting the query before INSERTing the data and using a single query to INSERT, thus By building the query within the foreach loop, and running a single SQL query, the  Original Answer, multiple Parallel Inserts into Database. You can do this in parallel using TPL, e.g. specifically with the localInit overload of Parallel.ForEach. You will almost certainly want to look at throttling the amount of parallelism by tweaking MaxDegreeOfParalelism so that you don't inundate your database:


if your the keys from $seat_id and $seat_label match you can do it with only one foreach like this:

foreach($seat_ids as $seat_key => $seat_id ) {
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_label[$seat_key],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
}

Multiple foreach statements - PHP, I'm using it as an insert into a database: foreach($value as $value1) { foreach ($​info as $info2) { $sql = "INSERT INTO table_name (column1,column2) VALUES  I'm using Postgres, and I have a large number of rows that need to be inserted into the database, that differ only in terms of an integer that is incremented. Forgive what may be a silly question, but I'm not much of a database guru. Is it possible to directly enter a SQL query that will use a loop to programatically insert the rows?


Considering that you are using JSON to submit the form data and order among array is maintained.

You may try :

$i = 0;
foreach($seat_ids as $seat_id )
{
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_labels[$i],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
    $i++;
}

INSERT into a Table from within a WHILE/FOREACH loop?, I have a Table value stored as array (121-1231-4323-2, where 121 is the and split these and echo them out O.K., but I am having trouble inserting the values into an… <?php $db_host = "localhost"; // Place the username for the MySQL database You should really be using [fphp]mysqli[/fphp] or [fphp]pdo[/fphp] instead. Ok I have been working on this for a long time and I am just plain stuck. What I am developing is an online application for a school to keep track of missing assignments that students have. I have a table that lists the students first name, last name, homeroom, and last period teachers. Each student


Currently the form does not have a seat-label relation. if you do not tick all ids, then the labels after that will correspond to the wrong id (i.e. the ids' posted array is smaller than the labels' array)

Hence you will have to introduce the necessary relation into the form names:

<input type="checkbox" name="seat[{index}][id]" value="{id}">
<input type="hidden" name="seat[{index}][label]" value="{label}">

If that submits, you can now check if both boxes were ticked

// using $_POST for simplicity
$seats = array_filter($_POST['seat'], function (array $row) {
    return count($row) === 2;
});

Now you have an array with corresponding ids/labels where only one loop is needed.

EDIT: overlooked that the second input is a hidden field, but that didn't affect the problem at all.

[Solved] How do I insert muntiple rows into mysql database using an , I i'm trying to insert multiple row of data into the database using an array and a foreach loop in php the data are collected from a form with  PHP Code to INSERT Data Into MySQL Database. There are two methods you can use to INSERT data into your MySQL database. The PHP MySQLi method and PHP Data Object or PDO method. MySQLi Method. First, you’ll need to establish a connection to a database. After that is done, we can proceed with the MySQL query INSERT. Here is a full PHP code


PHP Using a For Loop to do a Multi Row Insert into Mysql DB , Using a for loop to do a multirow insert into a MySQL database. PHP Using a For Loop to Duration: 6:36 Posted: Apr 25, 2018 You can use the INSERT statement to query data from one or more tables and insert it into another table as follows: INSERT INTO table1 (column1, column2) SELECT column1, column2 FROM table2 WHERE condition1; In this syntax, you use a SELECT which is called a subselect instead of the VALUES clause .


The List of the 10 Most Common Mistakes That PHP Developers , Using references in foreach loops can be useful if you want to operate on 2 million rows for ($row = 0; $row < 2000000; $row++) { $query = "INSERT For example, this is the way Zend\Db\TableGateway of Zend Framework 2 returns data  Add (Insert) rows in DataTable using For Each loop using C# and VB.Net When the Copy Button is clicked, a new DataTable is created with columns same as that of the GridView. Then a loop is executed and one by one each GridView Row is copied / added / transferred to the DataTable.


How to insert more than one row of data into a table at one time , You could use a foreach loop - function mp_install_name_data() { global $wpdb; $table_name = $wpdb->prefix . "names"; $rows = array( array( 'id' => '1',  Pay particular interest to the filename column and you will see that the query executed from within the context of the master database (where I ran the query from) for each database in the SQL instance (as noted by the database name being returned via the use of the "?" placeholder).