Using the upload functionality but it is inserting two records in the DB

Using the upload functionality but it is inserting two records in the DB

devincoxdevincox Posts: 4Questions: 1Answers: 0

The form is working as expected except for when the information is to be recorded in the DB. When you click "choose file" and then select the file you want to upload , a record gets created in the DB with the information for the file to be uploaded. Then when I click "Create" another record is created instead of just updating the first record. Neither record has all the information from the form. Im not sure if there is some config error here or what. Has anyone seen this behavior before?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Yep, that shouldn't be the case, it should create a new record on upload, and nothing more.

    Please can you post your Editor initialisation code, and your server script if it's been modified,

    Colin

  • devincoxdevincox Posts: 4Questions: 1Answers: 0
    edited August 2021
    $(document).ready(function() {
          editor = new $.fn.dataTable.Editor( {
            ajax: "./table.upload.php",
            table: "#mainDataTable",
            idSrc:  'upload.uploadID',
            fields: [ {
                    label: "Upload Type ID:",
                    name: "uploadTypeID"
                  },{
                label: "Client ID:",
                name: "upload.clientID"
                },{
                  label: "Upload ID:",
                  name: "upload.uploadID",
                  type: "upload",
                  display: function ( file_id ) {
                      return '<img src="'+editor.file( 'upload', file_id ).web_path+'"/>';
                  },
                  clearText: "Clear",
                  noImageText: 'No image'
              }
            ]
          } );
    
    
    
    Editor::inst( $db, 'upload', 'uploadID' )
        ->debug( true )
        ->fields(
            Field::inst( 'upload.uploadTypeID' ),
            Field::inst( 'upload.clientID' ),
            Field::inst( 'upload.insertDate' ),
            Field::inst( 'upload.uploadID' )
                ->setFormatter( Format::ifEmpty( null ) )
                ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__ID__.__EXTN__' )
                    ->db( 'upload', 'uploadID', array(
                        'filename'    => Upload::DB_FILE_NAME,
                        'filesize'    => Upload::DB_FILE_SIZE,
                        'web_path'    => Upload::DB_WEB_PATH,
                        'system_path' => Upload::DB_SYSTEM_PATH
                    ) )
                    ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
                    ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                )
    
        )
        ->process( $_POST )
        ->json();
    
  • allanallan Posts: 61,627Questions: 1Answers: 10,090 Site admin
    Answer ✓

    The way the Editor upload works is that it is async to the rest of the form. The file is uploaded as saved as soon as it is selected. The server should then return an id for that file (usually the primary key value) which is used as the value for the form.

    The key to this working is that you need two different database tables - one for the list of records shown in the DataTable, and one for the files which are uploaded. The list of records then references the files table.

    You can see that here were the users table is the list of records and files is the db table for the files.

    In your own case you have upload for the table name for both, which is why it isn't working. You need to split the two out. If it has a 1:1 mapping, then a left join between them will do the job.

    Allan

  • devincoxdevincox Posts: 4Questions: 1Answers: 0

    thank you Allan, I'll give this a try.

  • devincoxdevincox Posts: 4Questions: 1Answers: 0

    Sorry for the late response. This worked. Thank you.

Sign In or Register to comment.