473,396 Members | 1,655 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,396 software developers and data experts.

Multiple MYSQL INSERT statement

12
Hi Im doing a questionnaire in PHP and MYSQL

I am trying to do a multiple insert using the following code

[PHP]$query1 = "INSERT INTO answers (answer_score,question_id,user_id) VALUES ('$item1','$question[0]','$user_id_foreign'), ('$item2','$question[1]','$user_id_foreign')";[/PHP]

But it will only insert the first value and not the second. I have tested the mysql bit of coding in mysql itself with some values I manuall input i.e.: -

(4, 1, 1), (3, 2, 1) and this inserted both sets of values. I have tried splitting them up into seperate queries but the exact same thing happens.

From what I can tell I believe it is to do with my values for the user_id, I am inserting the user_id with a variable I have called $user_id_foreign. Due to the lack of proper foreign keys in the versions I have (v 4.1) I am having to count up the number of users in the table put them in DESC order and then add 1 for the next user_id. This works like a charm but as there are 75 questions I need this same ID to be repeated 75 times.

Another possibility is that of the answers (1 to 7) which are stored as strings in $item1, $item2 and so on till $item75

Any help would be gratelly appreciated

Thanks

Nabz
Mar 3 '07 #1
2 4027
Hi.

It seems that the '$foreign_user_id' has the same value in both records. So 1st record is inserted while 2nd is not.

As you are adding 1 to your user id for each record you should do something like

Expand|Select|Wrap|Line Numbers
  1.  
  2. $query1 = "INSERT INTO answers (answer_score,question_id,user_id) VALUES"
  3.  
  4. for each record
  5.  
  6. $item = item of question num
  7. $Question = question num
  8. etc
  9.  
  10.  
  11. $query1  =$query1 . " ('$item','$question','$user_id_foreign'), "
  12.  
  13. increment user_id_foreign
  14.  
  15. end for
  16.  
  17.  
  18.  
Mar 5 '07 #2
javedna
12
Hi.

It seems that the '$foreign_user_id' has the same value in both records. So 1st record is inserted while 2nd is not.

As you are adding 1 to your user id for each record you should do something like

Expand|Select|Wrap|Line Numbers
  1.  
  2. $query1 = "INSERT INTO answers (answer_score,question_id,user_id) VALUES"
  3.  
  4. for each record
  5.  
  6. $item = item of question num
  7. $Question = question num
  8. etc
  9.  
  10.  
  11. $query1  =$query1 . " ('$item','$question','$user_id_foreign'), "
  12.  
  13. increment user_id_foreign
  14.  
  15. end for
  16.  
  17.  
  18.  

Cheers mate

That worked like a charm

Thanks

Nabz
Mar 6 '07 #3

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
6
by: Rudi Ahlers | last post by:
A different question though. Is it possible to delete duplicate entries, where the email address is the same, and only keep one? i.e.. I got say 4 DB entries, all with the same email address,...
0
by: Michael Schoen | last post by:
Hi there, i=B4m actually developing a performance criticial system where I need to index a huge amount of protocoll data. These data are already in a chronological order, so if I push the data...
3
by: Tim Hastings | last post by:
Hello, I am using MyODBC from VB and I want to submit a batch of insert statements in one call from my App. This is more efficient than making multiple calls from code because of the...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
3
by: kristof.loots | last post by:
Hi all, Somehow I want to transfer data from an mysql database on the web to an local offline access database. Note: tables and types do not match. So, I toughed, generating sql commands to...
3
Atli
by: Atli | last post by:
Hi. I've been trying to insert multiple rows into a table using a single INSERT statement in MSSQL / SQL Server 2005. I could of course cheat and have my C# code insert each row using some sort...
1
by: paulq182 | last post by:
PLEASE HELP ME WITH MY CODE?? import java.sql.*; import java.io.*; class min_filmdb_rel_mysql { public static void main (String args ) throws SQLException, IOException {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.