473,396 Members | 1,755 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.

one submission of multiple files,multiple ids instead of one.

58
Hi all !
I hope someone help me with problem that I assume common,yet googling didnt even mention it.
In brief:
User uploads multiple files at one go.
It is one submission of multiple files,so I just need one id record for all those files in mysql.Now I get a corresponding number of ids instead of just one
I tried this,but for some reason it didnt work for me


Expand|Select|Wrap|Line Numbers
  1. $idquery = "SELECT * FROM file_uploads ORDER BY id ASC LIMIT 1";
  2. $idresult = mysql_query($idquery) or die('error, get last id query failed: ' . mysql_error());
  3. if($idrow = @mysql_fetch_array($idresult)){
  4.     $idpost = $idrow["id"]++;
  5. }else{
  6.     $idpost = 0;
  7. }
  8.  
  9. 1.  $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$idpost’,'$name','$type','$size','$file_data','$    Capt  ion)";
  10. 2.  mysql_query($query) or die('error, insert query failed:'.mysql_error());
  11. 3.  mysql_close($conn);
  12. 4.  }
  13.  
  14.  
But it only let all files be uploaded except the last one,then I got this error:
error, insert query failed:Duplicate entry '1' for key 1

Changing


Expand|Select|Wrap|Line Numbers
  1. $idpost = $idrow["id"]++;
  2.  
to:


Expand|Select|Wrap|Line Numbers
  1. $idpost = $idrow["id"]+1;
  2.  
Would only give me:
error, insert query failed:Duplicate entry '2' for key 1.
Jul 17 '07 #1
6 2835
Atli
5,058 Expert 4TB
If you are attempting to insert multiple items using the same ID, it won't work. Primary keys, which I assume your ID's are, are supposed to be unique. A way to identify a single row out of the rest, even if all other fields in the table are identical to each other.

The error you are getting is being shown because you are trying to break this rule by inserting duplicate values as primary keys.

If you create a integer primary key you can define it as an AUTO_INCREMENT field, which will allow MySQL to automatically insert the next value into the field every time you insert a row. You don't even have to include the field in the insert statement, it is inserted by default.
Jul 17 '07 #2
Jankie
58
That's correct,Atli
Thank you for explaining to me the very obvious :)
My problem is that am trying to relate these multiple uploads in table 2 to the submitter's other details in table 1.
Say Joe submits a form that collects both his name(in table1) and his uploads(in table2)
In table 1,I will have:
id
1
in table 2,I will have(suppose he uploads 3 files):
id
1
2
3
Of course,last_insert_id will only work for one id(as primary key are unique).Having no last_insert_id,means no relational mechanism for my tables.
I would prefer a solution without a foreing key,if possible
Has anyone come across this problem before?
Jul 17 '07 #3
Atli
5,058 Expert 4TB
Why don't you want a foreign key?
There is no real way of connecting two tables without putting a foreign key somwhere.

My solution to this would be (and has been) something like this:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE tblUser (
  2.   UserID SERIAL Primary Key,
  3.   UserName VARCHAR(255) NOT NULL
  4.   /* And whatever other info you might need */
  5. );
  6.  
  7. CREATE TABLE tblImage (
  8.   ImageID SERIAL Primary Key,
  9.   ImageOwner BigInt References tblUser(UserID),
  10.   ImageData MediumBlob NOT NULL
  11.   /* And whatever other info you might need */
  12. );
  13.  
And the PHP to poppulate the tables:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach($_FILES['arrName']['Error'] as $key => $error) 
  3. {
  4.     // Make sure the image was uploaded propperly
  5.     if(abs($error) != 0) {
  6.         echo "<p>File $key failed to upload ($error)";
  7.         continue;
  8.     }
  9.  
  10.     // Read the file contents
  11.     $path = $_FILES['arrName']['tmp_name'][$key];
  12.     $fh = fopen($path, "r");
  13.     $contents = fread($fh, filesize($path));
  14.     fclose($fh);
  15.  
  16.     // Insert into the Database
  17.     $QUERY = "INSERT INTO tblImage(ImageOwner, ImageData) VALUES({$_SESSION['UserID']}, '$contents')";
  18.     $RESULT = @mysql_query($QUERY);
  19.  
  20.     // Print the results
  21.     if(!!$RESULT) {
  22.         echo "Image $key loaded successfully";
  23.     } else {
  24.         echo"File $key failed to load<pre><b>SQL Error</b>\r\n". mysql_error() ."</pre>";
  25.     }
  26. }
  27. ?>
  28.  
Jul 18 '07 #4
Jankie
58
Thanks a lot,Atli
I implement what you gratefully suggested and got this error:
error, insert query failed:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''dosfb0d355592be4.doc','application/msword','34816','��ࡱ�\0\

Am using php4.3.10

I assume,it's php version-related-php4.3.10 does not like ({$_SESSION['submission_id']},) but am not sure.

So I put it as follows:
Expand|Select|Wrap|Line Numbers
  1. $submission_id =$_SESSION['submission_id'];
  2.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['upload']) && $_FILES['user_image']['size'] > 0){
  3.  
  4. $submission_id =$_SESSION['submission_id'];
  5. $fname = $_POST['fname'];
  6. $lname = $_POST['lname'];
  7. $address = $_POST['address'];
  8. $email = $_POST['email'];
  9.  
  10. include 'conn.php';
  11.  
  12. $query = "INSERT INTO table1 (submission_id,fname,lname,address,email) VALUES ('$submission_id','$fname','$lname','$address','$email')";
  13. mysql_query($query) or die('error, insert query failed: ' . mysql_error());
  14. include 'thankyou.php';
  15.  
  16. foreach ($_FILES["image"]["error"] as $key => $error) {
  17. if($_FILES['userfile']['error'][$key] != 0) {
  18.   continue;
  19. }
  20.  
  21. $filename = $_FILES['user_image']['name'][$key];
  22. $tempname  = $_FILES['user_image']['tmp_name'][$key];
  23. $filesize = $_FILES['user_image']['size'][$key];
  24. $filetype = $_FILES['user_image']['type'][$key];
  25. $fp = fopen($tmpName, 'r');
  26. $image  = fread($fp, filesize($tmpName));
  27. $image = addslashes($image);
  28. fclose($fp);
  29.  
  30. $uploaddir = 'image_uploads/';
  31. $upload_path = $uploaddir.basename($_FILES['user_image']['name'][$key]);
  32.  
  33. $query = "INSERT INTO images (imageOwner,name,type,size,image,Caption) VALUES ('$submission_id','$filename','$filetype','$filesize','$image','$Caption')";
  34. mysql_query($query) or die('error, insert query failed:'.mysql_error());
  35. mysql_close($conn);
  36.  
  37. //other stuff here
  38. //other stuff here
  39. ?>
  40.  
it gives me for imageOwner
id
0
Jul 19 '07 #5
Atli
5,058 Expert 4TB
It's most likely that this is caused by the quote marks arround the 'submission_id' and the 'filesize' in your second INSERT statement.
Numeric values should not be put inside quote marks, only strings.

That is; this line:
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO images (imageOwner,name,type,size,image,Caption) VALUES ('$submission_id','$filename','$filetype','$filesize','$image','$Caption')";
  2.  
Should look like this:
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO images (imageOwner,name,type,size,image,Caption) VALUES ($submission_id,'$filename','$filetype',$filesize,'$image','$Caption')";
  2.  
Jul 19 '07 #6
-------error, insert query failed:Duplicate entry '2' for key 1.[/quote]
hi,
Always this error is with database you r trying to enter values into.
plz check out for proper fields selected,
VALUES (‘$idpost’,'$name','$type','$size','$file_data','$ Capt ion)";

there is a space getween.

this errer comes onlt when u have 2 key values for same field.
edit your db table.




Hi all !
I hope someone help me with problem that I assume common,yet googling didnt even mention it.
In brief:
User uploads multiple files at one go.
It is one submission of multiple files,so I just need one id record for all those files in mysql.Now I get a corresponding number of ids instead of just one
I tried this,but for some reason it didnt work for me


Expand|Select|Wrap|Line Numbers
  1. $idquery = "SELECT * FROM file_uploads ORDER BY id ASC LIMIT 1";
  2. $idresult = mysql_query($idquery) or die('error, get last id query failed: ' . mysql_error());
  3. if($idrow = @mysql_fetch_array($idresult)){
  4.     $idpost = $idrow["id"]++;
  5. }else{
  6.     $idpost = 0;
  7. }
  8.  
  9. 1.  $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$idpost’,'$name','$type','$size','$file_data','$    Capt  ion)";
  10. 2.  mysql_query($query) or die('error, insert query failed:'.mysql_error());
  11. 3.  mysql_close($conn);
  12. 4.  }
  13.  
  14.  
But it only let all files be uploaded except the last one,then I got this error:
error, insert query failed:Duplicate entry '1' for key 1

Changing


Expand|Select|Wrap|Line Numbers
  1. $idpost = $idrow["id"]++;
  2.  
to:


Expand|Select|Wrap|Line Numbers
  1. $idpost = $idrow["id"]+1;
  2.  
Would only give me:
error, insert query failed:Duplicate entry '2' for key 1.
Sep 20 '07 #7

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

Similar topics

6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
1
by: wcc | last post by:
Hello group, Can I use EasyDialogs to select multiple files? I saw the function AskFileForOpen but which does not have a flag allowing selection of multiple files. Did I miss something? ...
3
by: andrei | last post by:
Hi Group, In my program, the user has to be able to add one or more documents (as files) for one product from the database. The number of files to be uploaded can vary from 1 to maybe 30-40 and...
5
by: Stanav | last post by:
Hello all, Thanks in advance for any replies... Now, my question is: Is it possible to do a multiple files download for a single response event on an aspx page? If there is, please give me some...
5
by: Rothariger | last post by:
Hello.... i want to know if its posible to rename multiple files like windows does.. example: file zzzzzzz.doc file asdasd.doc file esfsefse.doc
1
by: Sönke Greve | last post by:
Hi there, i want to enable uploading lots of images and later display it as an online gallery. Therefore i need a way of using the FileUpload Component with selecting multiple files or a whole...
3
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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,...

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.