473,414 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

simultaneous inserting of row in a databse

is it possible to insert, say for example, 5 rows of records in a table?

is this possible?
Expand|Select|Wrap|Line Numbers
  1. $query1 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  2.  
  3. $query2 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  4.  
  5. $query3 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  6.  
  7. $query4 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  8.  
  9. $query5 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  10.  
Oct 3 '07 #1
8 1385
code green
1,726 Expert 1GB
I think I understand your problem.
Well to start, this is the SQL for multiple record INSERT
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tbl_items (item_code, description) VALUES
  2. ('$item_code1', '$description1'),
  3. ('$item_code2', '$description2'),
  4. ('$item_code3', '$description3'),
  5. ('$item_code4', '$description4')
But I understand that $item_code', '$description' variables are changing within your code, but you can approach this in a variety ways.
Build up and create the query within a loop by concatenating strings and variables.
Make $item_code and $description arrays then either extract and concatenate as above or
explode the arrays into the VALUES part of the query.
The best method will depend on the structure of your code so far, but I hope this gives you something to think about,
Oct 3 '07 #2
ok. thanks for the tip.

could I also use this code?
Expand|Select|Wrap|Line Numbers
  1. $query1 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  2.  
  3. if(query1)
  4. {
  5.      $query2 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  6.           if(query2)
  7.           {
  8.                $query3 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  9.                if(query3)
  10.                {
  11.                     $query4 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  12.                          if(query4)
  13.                          {
  14.                               $query5 = mysql_query("INSERT INTO tbl_items (item_code, description) VALUES ('$item_code', '$description')");
  15.                               if(query5)
  16.                               {
  17.                                 echo "insert success";
  18.                               }
  19.                          }
  20.                  }
  21.        }
  22. }
  23.  
Or will this code even work?
Oct 4 '07 #3
Atli
5,058 Expert 4TB
Hi.

Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

Moderator
Oct 4 '07 #4
Atli
5,058 Expert 4TB
Both of the code examples you have posted will work. But they are a little to complicated for what you are trying to do.

As code green said, you can insert multiple rows in a single INSERT statement.

This would do exactly the same thing both your previous code examples do, this would probably be a little faster though:
Expand|Select|Wrap|Line Numbers
  1. $query = "
  2. INSERT INTO tbl_items (item_code, description) VALUES 
  3. ('$item_code', '$description'), 
  4. ('$item_code', '$description'), 
  5. ('$item_code', '$description'), 
  6. ('$item_code', '$description'), 
  7. ('$item_code', '$description')";
  8.  
  9. $result = mysql_query($query);
  10.  
Oct 4 '07 #5
what if right after inserting the data, I want to get the item code and pass it to another table. say for example,

Expand|Select|Wrap|Line Numbers
  1. $query = "
  2. INSERT INTO tbl_items (item_code, description) VALUES 
  3. ('$item_code', '$description'), 
  4. ('$item_code', '$description'), 
  5. ('$item_code', '$description'), 
  6. ('$item_code', '$description'), 
  7. ('$item_code', '$description')";
  8.  
  9. $result = mysql_query($query);
  10.  
  11. if($result){
  12.       mysql_query(INSERT INTO inventory (item_code) VALUES ("$item_code"))";
  13. }
  14.  
is this possible?
Oct 4 '07 #6
code green
1,726 Expert 1GB
[PHP]$query = "
INSERT INTO tbl_items (item_code, description) VALUES
('$item_code', '$description'),
('$item_code', '$description'),
('$item_code', '$description'),
('$item_code', '$description'),
('$item_code', '$description')";
$result = mysql_query($query);
if($result){
mysql_query("INSERT INTO inventory (item_code) VALUES ("$item_code"))";
}
}[/PHP]
is this possible?
Not sure what you are doing here.
$item_code and $description have only one value throughout.
You are inserting the same values 5 times.
And then inserting the same value of $item_code in to inventory
Oct 4 '07 #7
Atli
5,058 Expert 4TB
is this possible?
Of course this is possible. Everything is possible! (within reason)

Try setting up a test server (if you haven't already) and just try it out. It will be a lot quicker than asking us. It's a much better way to learn.
Oct 4 '07 #8
Of course this is possible. Everything is possible! (within reason)

Try setting up a test server (if you haven't already) and just try it out. It will be a lot quicker than asking us. It's a much better way to learn.
I've already done that. It did worked. Thanks. Xiexie. Arigato. Salamat.
Oct 5 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Claudio Grondi | last post by:
I would like to save time copying the same file (>6 GByte) to various different target storage media connected to the system via USB. Is there a (Python or other) tool able to help me to do...
1
by: slugger | last post by:
Hope this is not OT: I am running into some strange things whenever my ASP pages send out simultaneous requests to another ASP page which in turn gains access to a MySQL database using a DSNless...
6
by: Jimnbigd | last post by:
I want to write a game, and sounds will really add to it. Note that I would always make the sounds optional. I hate it when I go to a URL and unexpectedly get sounds or music. I have played...
1
by: Semaj | last post by:
Environment: DB2 8.1.4; Windows 2000 We are evaluating the feasibility of upgrading our production DB from 7.2 to 8.1. During this process we've encountered an error when starting our...
5
by: Dexter | last post by:
Hello all, I need all my developers work at a project simultaneous. How it is possible? I need a machine as web server? And how to configure the visual studio to all to work simultaneous? ...
12
by: Dan V. | last post by:
Since an ASP.NET/ADO.NET website is run on the server by a single "asp_net worker process", therefore doesn't that mean that even 50 simultaneous human users of the website would appear to the...
3
by: Marco Herrn | last post by:
Hi, I have a text file with some lines in it. Now I want to iterate over this file and exchange some lines with some others. I tried this approach: try: myfile= file('myfile', 'r+') while...
2
by: aacc1313 | last post by:
Hi - with the Jython site being down, info here on the following has been difficult to come by! I'm trying to insert a unicode string into the following table: CREATE TABLE `p1` ( `id` int(11)...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.