467,134 Members | 1,050 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

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

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
  • viewed: 2394
Share:
6 Replies
Atli
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
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
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
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
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

Post your reply

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

Similar topics

1 post views Thread by wcc | last post: by
3 posts views Thread by andrei | last post: by
5 posts views Thread by Stanav | last post: by
1 post views Thread by Sönke Greve | last post: by
3 posts views Thread by Klaas Vantournhout | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.