Issue when trying to upload file
Issue when trying to upload file
I have a table named users in this table at some point I have a field named profile_pic. In this field all I store is a filename that is based on the user_id which is my primary key. e.g. user #3's profile pic would be named 3.jpg and 4's would be 4.jpg. I started by trying to use the file upload example https://editor.datatables.net/examples/advanced/upload.html
In my php file I have the following
Field::inst( 'profile_pic' )
->setFormatter( 'Format::nullEmpty' )
->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'../../upload/profile_pics/__ID__.__EXTN__' )
->db( 'users', 'user_id', array(
'profile_pic' => Upload::DB_FILE_NAME
) )
->allowedExtensions( [ 'jpg' ], "Please provide a .jpg image" )
),
My initial issue is that I get a SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'email' I know what that mean but i was updating the profile_pic field not the email field. ideas
This question has accepted answers - jump to:
Answers
This will store the name of the file that was uploaded. It it not store the file name that you have created (
__ID__.__EXTN__
). To be honest, if you wantprofile_pic
to contain just the id, it is redundant since you already have that information inuser_id
. Just concat.jpg
to the id when you request the file.I don't see
email
being used anywhere in the above code, so I'm not sure why that would be happening. Can you show the full code please?Allan
Excuse the Greek Localization but I am guessing you don't mind it
My PHP code in case you need it
My updated php code now look like this. I am hoping now the name will be based on the user_id. That duplicate error from mysql is still there so I am stuck
So to check my understanding - when you upload a file, in response to the upload Ajax request you get "Integrity constraint violation: 1062 Duplicate entry '' for key 'email'"?
Yes the steps I do are as follows
1. I select a row end click edit
2. I get the modal that allows for editing and I click on "choose file" and I select a file of the required format
3. Then I get the error
Thanks for the clarification.
I think the issue is that you are using the
users
table both as a file store (line 26) and as store for the user information (line 17).Have you have a separate database table that will store the information about the file and just left join onto it? That would then match the Editor examples.
Allan
I haven't although I saw that in the examples. I will see what I can do because the database I am using is also connected to something else which I was hoping not to change. In any case I will have a think so that I can use a different table. If it works out I will post back. Thanks for the advice either way
Using a second table really really helped but I really need to use the primary key of my initial table in the filename.
I have a table now called images I store the filenames and an id in there that is auto increment as the primary key. As you can see in line #23-24 I have
What I actually need is $id = user_id (which is the primary of table users). To be more precise:
Any ideas?
The above code actually does exactly what I need thanks @allan I just wasn't paying attention to the changes in the db