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 -
$idquery = "SELECT * FROM file_uploads ORDER BY id ASC LIMIT 1";
-
$idresult = mysql_query($idquery) or die('error, get last id query failed: ' . mysql_error());
-
if($idrow = @mysql_fetch_array($idresult)){
-
$idpost = $idrow["id"]++;
-
}else{
-
$idpost = 0;
-
}
-
-
1. $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$idpost’,'$name','$type','$size','$file_data','$ Capt ion)";
-
2. mysql_query($query) or die('error, insert query failed:'.mysql_error());
-
3. mysql_close($conn);
-
4. }
-
-
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 -
$idpost = $idrow["id"]++;
-
to: -
$idpost = $idrow["id"]+1;
-
Would only give me:
error, insert query failed:Duplicate entry '2' for key 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.
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?
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: -
CREATE TABLE tblUser (
-
UserID SERIAL Primary Key,
-
UserName VARCHAR(255) NOT NULL
-
/* And whatever other info you might need */
-
);
-
-
CREATE TABLE tblImage (
-
ImageID SERIAL Primary Key,
-
ImageOwner BigInt References tblUser(UserID),
-
ImageData MediumBlob NOT NULL
-
/* And whatever other info you might need */
-
);
-
And the PHP to poppulate the tables: -
<?php
-
foreach($_FILES['arrName']['Error'] as $key => $error)
-
{
-
// Make sure the image was uploaded propperly
-
if(abs($error) != 0) {
-
echo "<p>File $key failed to upload ($error)";
-
continue;
-
}
-
-
// Read the file contents
-
$path = $_FILES['arrName']['tmp_name'][$key];
-
$fh = fopen($path, "r");
-
$contents = fread($fh, filesize($path));
-
fclose($fh);
-
-
// Insert into the Database
-
$QUERY = "INSERT INTO tblImage(ImageOwner, ImageData) VALUES({$_SESSION['UserID']}, '$contents')";
-
$RESULT = @mysql_query($QUERY);
-
-
// Print the results
-
if(!!$RESULT) {
-
echo "Image $key loaded successfully";
-
} else {
-
echo"File $key failed to load<pre><b>SQL Error</b>\r\n". mysql_error() ."</pre>";
-
}
-
}
-
?>
-
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: -
$submission_id =$_SESSION['submission_id'];
-
-
<?php
-
if(isset($_POST['upload']) && $_FILES['user_image']['size'] > 0){
-
-
$submission_id =$_SESSION['submission_id'];
-
$fname = $_POST['fname'];
-
$lname = $_POST['lname'];
-
$address = $_POST['address'];
-
$email = $_POST['email'];
-
-
include 'conn.php';
-
-
$query = "INSERT INTO table1 (submission_id,fname,lname,address,email) VALUES ('$submission_id','$fname','$lname','$address','$email')";
-
mysql_query($query) or die('error, insert query failed: ' . mysql_error());
-
include 'thankyou.php';
-
-
foreach ($_FILES["image"]["error"] as $key => $error) {
-
if($_FILES['userfile']['error'][$key] != 0) {
-
continue;
-
}
-
-
$filename = $_FILES['user_image']['name'][$key];
-
$tempname = $_FILES['user_image']['tmp_name'][$key];
-
$filesize = $_FILES['user_image']['size'][$key];
-
$filetype = $_FILES['user_image']['type'][$key];
-
$fp = fopen($tmpName, 'r');
-
$image = fread($fp, filesize($tmpName));
-
$image = addslashes($image);
-
fclose($fp);
-
-
$uploaddir = 'image_uploads/';
-
$upload_path = $uploaddir.basename($_FILES['user_image']['name'][$key]);
-
-
$query = "INSERT INTO images (imageOwner,name,type,size,image,Caption) VALUES ('$submission_id','$filename','$filetype','$filesize','$image','$Caption')";
-
mysql_query($query) or die('error, insert query failed:'.mysql_error());
-
mysql_close($conn);
-
-
//other stuff here
-
//other stuff here
-
?>
-
it gives me for imageOwner
id
0
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: -
$query = "INSERT INTO images (imageOwner,name,type,size,image,Caption) VALUES ('$submission_id','$filename','$filetype','$filesize','$image','$Caption')";
-
Should look like this: -
$query = "INSERT INTO images (imageOwner,name,type,size,image,Caption) VALUES ($submission_id,'$filename','$filetype',$filesize,'$image','$Caption')";
-
-------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 -
$idquery = "SELECT * FROM file_uploads ORDER BY id ASC LIMIT 1";
-
$idresult = mysql_query($idquery) or die('error, get last id query failed: ' . mysql_error());
-
if($idrow = @mysql_fetch_array($idresult)){
-
$idpost = $idrow["id"]++;
-
}else{
-
$idpost = 0;
-
}
-
-
1. $query = "INSERT INTO file_uploads (id,name,type,size,file_data,Caption) VALUES (‘$idpost’,'$name','$type','$size','$file_data','$ Capt ion)";
-
2. mysql_query($query) or die('error, insert query failed:'.mysql_error());
-
3. mysql_close($conn);
-
4. }
-
-
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 -
$idpost = $idrow["id"]++;
-
to: -
$idpost = $idrow["id"]+1;
-
Would only give me:
error, insert query failed:Duplicate entry '2' for key 1.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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? ...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: 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,...
| | |