Skip to main content
April 25, 2007
Answered

Inserting Image Name into the database

  • April 25, 2007
  • 12 replies
  • 3283 views
I have created a form with a file field to upload an image and insert its name directly into the mysql database. The column type is varchar where the name will be inserted through the form. Now i am getting two problems. All the fields get filled out in the database except the image name. The image name must be inserted into the productImage column. It remanins null. No image is uploaded to the destination folder.

And also on submitting the form, I get this error.
Error Executing Database Query.
Column 'productID' cannot be null

I am attaching the full code. Could someone help out? All i require is that the file is uploaded and name inserted into mysql database in specified column.
    This topic has been closed for replies.
    Correct answer existdissolve
    I forgot about getting bck to you sooner.
    I tried the code and i feel it has some errors so i made some changes and i am still getting errors that The form field "ul_path1" did not contain a file.
    I had created a new table with only those 3 columns listed in the query and still not functioning.


    There are only two errors that I see in your code:

    First, you need cfoutput tags around the looping form---otherwise, the #i# naming of the fields will not work, and all three will simply be named "productID#i#", etc...

    Second, you need to dynamically name the variable "file", not cffile.Serverfile. As each file is uploaded, the cffile.Serverfile will be associated with the loop value of file.

    I am attaching your same code with the changes commented out so that you can see what needs to happen. BTW, I have tested this code, and it works as expected.

    Good luck

    12 replies

    existdissolve
    Inspiring
    April 25, 2007
    And also on submitting the form, I get this error.
    Error Executing Database Query.
    Column 'productID' cannot be null And also on submitting the form, I get this error.

    Is productID your primary key on that table? If so, set the column to auto increment, and you will not need to use a form field to insert its value.
    April 25, 2007
    To make things simple, I removed all the code except the code that makes up the main form. Now the file does upload to the destination folder but now i need it to be inserted into the database. I am not clear on a few issues. I am facing a few problems which I will explain.

    I am attaching the new code.

    <cfif isdefined("form.submit")>
    <cffile accept="image/*" action="upload" destination="C:\CFusionMX\wwwroot\976evil\Other_files\" filefield="ul_path" nameconflict="makeunique">
    <cfinsert datasource="#datasource#"
    tablename ="products"
    formfields="productImage">
    The file was successfully uploaded!
    </cfif>

    In the above code, I have used the cfinsert tag. Due to this, I get this error.
    Variable DATASOURCE is undefined.
    The image however is uploaded to the destination folder but none of the table columns is populated with the form data.
    If I use the name of the database inplace of datasource, i get another error stating that 976evil_db," on line 23, column 24, is not a valid identifer name. eg.<cfinsert datasource="#976evil_db#"..................

    Now when i remove or comment out the cfinsert code, on submitting the form, the table columns are populated this time with form data except image name and the image is uploaded to the destination directory as before. I am still getting the productID cannot be null error. What could be done to make this work?
    existdissolve
    Inspiring
    April 25, 2007
    Just to test things out, trying removing this section of code and submit the form like before:

    <cfinsert datasource="#datasource#"
    tablename ="products"
    formfields="productImage">
    The file was successfully uploaded!
    </cfif>
    <cfif isdefined("form.totalrows")>

    And also remove the closing <cfif> related to "totalrows". In other words, have only the condition, "FORM.submit" and see what happens.
    existdissolve
    Inspiring
    April 25, 2007
    What I do is upload the file and then Insert to the database on the same condition (e.g., <cfif isDefined("FORM.submit")>).

    That way, you can upload the file, and then use the #cffile.Serverfile# to populate the column in your database---no need to do that *after* you have inserted the rest of the form.

    So I personally do this with my forms like this:
    <cfset uploadfolder = #ExpandPath("yourpathhere")#>

    <cfif isdefined("form.submit")>
    <cffile action="upload" filefield="image1" destination="#uploadfolder#"
    nameconflict="overwrite" accept="image/*" >

    <cfquery datasource="#mydb#>
    INSERT into products
    (productID, Description, Brand, sizeOnOrder, productImage, individualCost, supplierID, individualWeight, unitWeight, unitsInStock, unitsOnOrder, quantityPerUnit, unitCost, shippingCostPerUnit, unitStandAloneCost)
    VALUES (......................
    <!---Rest of the columns here---->

    <!---Begin productImage insert--->
    <cfif isdefined("form.productImage#i#") and Evaluate("form.productImage#i#") NEQ ''>
    <cfqueryparam value="#cffile.Serverfile#" cfsqltype="cf_sql_varchar"/>
    <cfelse>
    NULL
    </cfif>,
    ......................)
    </cfquery>
    <cfif>

    So basically, instead of trying to get the filename from the form, you get it from the name of the file already on the server.

    Also, I don't see where you've defined #uploadfolder# for the destination...

    Good luck!