File upload: Store filename in database

Stroganoff 30 Jul, 2009
I'm want to store the file name of the uploaded file in the database. Currently the file gets uploaded and renamed successfully, but there is no entry in the database. All other data gets stored fine.
Example: The name of the input field is "logo" = the name of the database collumn where I want to store the filename.

I already found some info on this but it seemed to be outdated.

What should I do now? I guess I'll have to write some code (probably including "basename($attachments['logo'])") into "onSubmit Events Code" but I don't know what to write exactly.
GreyHead 30 Jul, 2009
Hi stroganoff,

If there is a table column that has the same name as the file input then the file name (but not the path) should be saved OK

Bob
Stroganoff 30 Jul, 2009
OK it's working now. (out of the box)
The problem was, that I added the file input field after creating the table. Then I added the new collumn manually via PhpMyAdmin (with VARCHAR(255)). Aparrantly ChronoForms was not automatically aware of the new collumn.
I'm wondering why it's behaving like that or where I can 'register' additional collumns to the form (I don't see any reference to this in Form Management).

Anyway, it's working now (i dropped the table and created a new one with ChronoForm).
nml375 30 Jul, 2009
Hi Stroganoff,
This is due to the way the DB Connection works.
To get down and dirty, there's a "hidden" field in the CF table known as dbclasses; whenever you update the selected table in the DB Connection, this is updated with a php class description of a JTable class matching this database table.
This class is then used in the auto-generated code to store the submitted data into your table.

As such, if you alter your database table, make sure to update your DB Connections tab aswell...

/Fredrik
Stroganoff 30 Jul, 2009
Alright, thanks for the explanation. You've been a tremendous help.🙂
Stroganoff 06 Aug, 2009
OK, last time I solved this by creating a new table (before reading your explanation).
Now again I added a new field to the form and it won't get stored in the database. I tried to follow your advice and double checked the generated code (form field names and types) and the content of "dbclasses" in "jos_chrono_contact" (var $foobar = null;) and found my new field referenced there correctly.

Editing the field manually in PhpMyAdmin works.

I'd rather not create a new table this time because I'd like to fully understand this issue.
What exactly am I missing here?


P.S. You might split this thread if this is preferable.
GreyHead 06 Aug, 2009
Hi Stroganoff,

What's the field name?

You can turn on Site Debug and look at the SQL that ChronoForms generates to see if the field/value is included there.

Bob
nml375 06 Aug, 2009
Hi Stroganoff,
Just to double-check that we didn't overlook anything;

I assume you updated your database table to have a new field named "foobar" (using phpmyadmin or such)?
(Since your updated JTable class contained a reference to $foobar).

Did you also add a new form input named "foobar" (explicitly "foobar") to your form?

If you enable debug within your form (Bob's suggestion is good too), do you see a name/value pair within the _POST "listing" named "foobar" with some value?


To get into the gory mechanics again... When we reach the point of the DB storage during the submission, we create a new JTable object based on the code in the dbclasses field. Next, we get a "safe" copy of the $_POST array, which is then "bound" to the JTable object. Binding the data simply means iterate through the array ($_POST name/value pairs), and if there is a property in the object with the same name, assign the value taken from the data to it.

Next, assuming all went fine, we call the store() method, which either adds a new record, or updates an already existing one (depends on whether there is a value for the primary key in the object), using all values in the object.

So, for all this to work, we need:
1. A value in $_POST[] with a name matching the field name in the database (that is, a form input with the same name).
2. A JTable object in dbclasses with a property named like the field name in the database.
3. A field in your database named like the above two.

/Fredrik
ms_buggy 02 May, 2011
Ok, I have similar problem with the file name.

If I change the input type to "text" from "file" value is stored.
But when I try to upload the file, file is uploaded but the name of the file is not stored.

if I understood correctly the tutorial, the name should be assigned only with the field names, so no further configuration is not needed..

Any advice apprichiated!
GreyHead 02 May, 2011
Hi ms_buggy,

This is a very old thread. Which version of ChronoForms are you using please?

Bob
ms_buggy 03 May, 2011
Thanks for the reply, and sorry for usign old thread..

ChronoForms version is 3.2.0
CF_Plugin version is 3.1_RC
And Connectivity is V2_RC3 (but is guess it doenst matter)

edit: Ok, I dont know how I got it working, but it works now.. I played back and forth for a while, and then it saved the file name also.
GreyHead 03 May, 2011
Hi ms_buggy,

Good to hear that you got it working. Thanks for letting us know.

Bob
katsikar 03 Feb, 2012
Hello guys,

Same problem for me on Joomla 2.5 with CF4 RC3.11. Table created by the wizard, table column name same as file upload name, file gets uploaded but no file name inserted in the DB column(actually "nothing" is inserted, because the default value is NULL). When inserting the debugger and enabling debug I see that nothing is passed from the element to the query:
debbuger:
[upimage]=>

SQLQuery:
INSERT INTO... ,`upimage`) VALUES (...,'')

Any suggestions? Is it be a code bug? Thanks in advance.
GreyHead 03 Feb, 2012
Hi katsikar,

I just ran a test and the file name is saved OK here. Do you have the File Upload action before the DB Save action? I'm not sure if that makes a difference but it might.

Please drag a Debugger action into the On Submit event, then submit the form and post the debug - including the 'dummy emails' results here. Note: if you are using the Easy Wizard you may need to switch to the Advanced Wizard to do this; if you want to continue to use the Easy Wizard please make a copy of your form first and add the Debugger action to the copy.

Bob
katsikar 04 Feb, 2012
Hi Bob,

Actually I have tried putting file upload first and DBsave in OnSuccess of file upload but it didn't save the name of the file. However, if I put DBSave after file upload as you suggested (not inside OnSuccess) it works!!! Same thing for the debbuger. If it is on the top of the OnSubmmit the upimage field is blank, but if it is on the bottom it gets the file name. Thanks a lot Bob, I’ve been stack here for three days.
GreyHead 04 Feb, 2012
Hi katsikar,

Well done. The actions are processed in order from top to bottom so you do need to plan the order to do what you need.

Bob
This topic is locked and no more replies can be posted.