472,145 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Insert multiple rows at once

Hello there,

i have a strange problem. I can't get php to insert multiple rows at once in
a MySQL database. I use the
$sql = "INSERT INTO database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',
'$e')";

I want to insert 5 rows at a time in the database, but it only inserts every
5th record. For example:
1. AA
2. AB
3. AC
4. AD
5. AE

I only find 5. AE back in the mysql with id 1.

How can I insert multiple rows at once ???

Thnx in advance

RotterdamStudents.
Jul 17 '05 #1
7 33123
On Mon, 16 Aug 2004 21:32:45 +0200, RotterdamStudents wrote:
Hello there,

i have a strange problem. I can't get php to insert multiple rows at once in
a MySQL database. I use the
$sql = "INSERT INTO database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',
'$e')";

These are columns, not rows.

I want to insert 5 rows at a time in the database, but it only inserts every
5th record. For example:
1. AA
2. AB
3. AC
4. AD
5. AE

I only find 5. AE back in the mysql with id 1.

How can I insert multiple rows at once ???

Thnx in advance

RotterdamStudents.

AFAIK, they have to be inserted separately with multiple SQL statements.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2
.oO(Ian.H)
On Mon, 16 Aug 2004 21:32:45 +0200, RotterdamStudents wrote:
How can I insert multiple rows at once ???

AFAIK, they have to be inserted separately with multiple SQL statements.


INSERT INTO foo (bar) VALUES ('bar1'), ('bar2'), ('bar3');

Micha
Jul 17 '05 #3
On Mon, 16 Aug 2004 22:19:50 +0100, Michael Fesser wrote:
.oO(Ian.H)
On Mon, 16 Aug 2004 21:32:45 +0200, RotterdamStudents wrote:
How can I insert multiple rows at once ???

AFAIK, they have to be inserted separately with multiple SQL statements.


INSERT INTO foo (bar) VALUES ('bar1'), ('bar2'), ('bar3');

Micha

ahah!

Thanks Micha.. this'll help me too =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4
"RotterdamStudents" wrote:
Hello there,

i have a strange problem. I can’t get php to insert multiple
rows at once in
a MySQL database. I use the
$sql = "INSERT INTO database (a,b,c,d,e) VALUES (’$a’,
’$b’ ,’$c’, ’$d’,
’$e’)";

I want to insert 5 rows at a time in the database, but it only inserts every
5th record. For example:
1. AA
2. AB
3. AC
4. AD
5. AE

I only find 5. AE back in the mysql with id 1.

How can I insert multiple rows at once ???

Thnx in advance

RotterdamStudents.


http://www.desilva.biz/mysql/insert.html

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Insert-m...ict140156.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=468729
Jul 17 '05 #5
On Mon, 16 Aug 2004 22:19:50 +0100, Michael Fesser <ne*****@gmx.net> wrote:
.oO(Ian.H)
On Mon, 16 Aug 2004 21:32:45 +0200, RotterdamStudents wrote:
How can I insert multiple rows at once ???

AFAIK, they have to be inserted separately with multiple SQL statements.


INSERT INTO foo (bar) VALUES ('bar1'), ('bar2'), ('bar3');


Although watch out as MySQL doesn't have any way of indicating which of the
rows caused an error, if there is one partway through.

And it's got some plain wrong behaviour as well:

mysql> insert into x values (null);
ERROR 1048: Column 'c' cannot be null

mysql> insert into x values ('aa'), (null);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from x where c is null;
Empty set (0.00 sec)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6
.oO(Andy Hassall)
And it's got some plain wrong behaviour as well:

mysql> insert into x values (null);
ERROR 1048: Column 'c' cannot be null

mysql> insert into x values ('aa'), (null);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0


This returns a warning on my system, but MySQL inserts an empty value
instead of the default value:

mysql> DESC foo;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| bar | varchar(10) | | | bar | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> INSERT INTO foo VALUES ('test'), (NULL);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 1

mysql> SELECT * FROM foo;
+------+
| bar |
+------+
| test |
| |
+------+
2 rows in set (0.00 sec)

Micha
Jul 17 '05 #7
> http://www.desilva.biz/mysql/insert.html

--

Thanks this works.

RotterdamStudents
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by wombat53 | last post: by
4 posts views Thread by Michel Esber | last post: by
24 posts views Thread by Henry J. | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.