|
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.
| |
Share:
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?
| | 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
| | 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.
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
6 posts
views
Thread by x. zhang |
last post: by
|
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
|
5 posts
views
Thread by Rothariger |
last post: by
|
1 post
views
Thread by Sönke Greve |
last post: by
|
3 posts
views
Thread by Klaas Vantournhout |
last post: by
| | | | | | | | | | | | | |