php foreach statement to insert data into sql only inserting last line

kevibu

I'm trying to use a foreach statement in PHP to insert data from an array into a SQL database. The array can vary from 40 to 80 lines of data. I'm using the following code:

foreach ($racelap as $lap){
     $sql = "insert into laps (RaceID,Lap,time,Temp,Humidity) ";
     $sql .= "values ($RaceID,'$lap[0]','$lap[1]',$lap[2],'$lap[3]')";
    mysql_query($sql); 
}

The code doesn't give me an error, but it only adds the last line of data, and ignores all other data.

The solution is probably simple, but I can't find it myself unfortunately.

Dave

Try this, use your query with insert multiple rows using a single SQL INSERT statement something like this :

INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

CODE :

$sql = "insert into laps (RaceID,Lap,time,Temp,Humidity) values  ";
foreach ($racelap as $lap){
     $sql .= "($RaceID,'$lap[0]','$lap[1]',$lap[2],'$lap[3]') ,";
}
$sql =rtrim($sql,",");
mysql_query($sql); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP Foreach SQL, INSERT INTO ON DUPLICATE KEY UPDATE only inserting last query?

From Dev

for each loop PHP, insert into SQL not inserting data

From Dev

for each loop PHP, insert into SQL not inserting data

From Dev

PHP Insert INTO Not Inserting Data

From Dev

PHP foreach banlist only checks last line of file

From Dev

PHP Insert Statement Inserting Array Values Twice

From Dev

PHP INSERT Prepared statement not inserting with ajax

From Dev

SQL in statement for last data

From Dev

insert EOF statement before the last line of file

From Dev

PHP PDO Insert Only Inserting First Character

From Dev

Insert to sqlite only runs the last statement

From Dev

sql statement query last data

From Dev

sql statement query last data

From Dev

PHP prepare statement insert too many in foreach

From Dev

PHP prepare statement insert too many in foreach

From Dev

PHP/SQL Insert data from form and insert data from another table using prepared statement

From Dev

SQL Insert Statement Inserting 2 records instead of 1

From Dev

Inserting data to mysql database using PDO with direct insert statement

From Dev

Inserting data to mysql database using PDO with direct insert statement

From Dev

An Exception of Type Syntax error in INSERT INTO statement was encountered while inserting the data

From Dev

PHP Foreach loop only displaying last result

From Dev

PHP json foreach returning only last record

From Dev

php Foreach Loop only extracting last record

From Dev

Inserting data dynamically in Temporary table by Select Statement only

From Dev

Using PHP to Insert Into a Database - only inserting one row

From Dev

Error when inserting data in mysql using php insert.php

From Dev

Mysqli Prepare Statement Insert Not Inserting

From Dev

Inserting Function into SQL Statement

From Dev

Sql insert query not inserting

Related Related

  1. 1

    PHP Foreach SQL, INSERT INTO ON DUPLICATE KEY UPDATE only inserting last query?

  2. 2

    for each loop PHP, insert into SQL not inserting data

  3. 3

    for each loop PHP, insert into SQL not inserting data

  4. 4

    PHP Insert INTO Not Inserting Data

  5. 5

    PHP foreach banlist only checks last line of file

  6. 6

    PHP Insert Statement Inserting Array Values Twice

  7. 7

    PHP INSERT Prepared statement not inserting with ajax

  8. 8

    SQL in statement for last data

  9. 9

    insert EOF statement before the last line of file

  10. 10

    PHP PDO Insert Only Inserting First Character

  11. 11

    Insert to sqlite only runs the last statement

  12. 12

    sql statement query last data

  13. 13

    sql statement query last data

  14. 14

    PHP prepare statement insert too many in foreach

  15. 15

    PHP prepare statement insert too many in foreach

  16. 16

    PHP/SQL Insert data from form and insert data from another table using prepared statement

  17. 17

    SQL Insert Statement Inserting 2 records instead of 1

  18. 18

    Inserting data to mysql database using PDO with direct insert statement

  19. 19

    Inserting data to mysql database using PDO with direct insert statement

  20. 20

    An Exception of Type Syntax error in INSERT INTO statement was encountered while inserting the data

  21. 21

    PHP Foreach loop only displaying last result

  22. 22

    PHP json foreach returning only last record

  23. 23

    php Foreach Loop only extracting last record

  24. 24

    Inserting data dynamically in Temporary table by Select Statement only

  25. 25

    Using PHP to Insert Into a Database - only inserting one row

  26. 26

    Error when inserting data in mysql using php insert.php

  27. 27

    Mysqli Prepare Statement Insert Not Inserting

  28. 28

    Inserting Function into SQL Statement

  29. 29

    Sql insert query not inserting

HotTag

Archive