copying image from one table to another

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 7, 2014   (RSS)

By markr - March 6, 2014

I can use php mysql insert code to copy a record from one table to another but I lose the image field. Is there a way to copy the image field to the new record?

By markr - March 6, 2014

Yes, I need the old record in table_1 and the duplicate record in table_2 to have the same image.

By gregThomas - March 7, 2014

Hi Markr,

Thanks for clarifying.

You'll need to use the saveUploadFromFilepath function, this will create a upload record for an item based on file stored on the server:


  //Get the record I'm going to copy
  $oldRecord = mysql_get('old_section', '1');

  //Select the uploads from the old section that belong to that record from the uploads table
  $oldUploads = mysql_select('uploads', array('recordNum' => $oldRecord['num'], 'tableName' => 'old_section' ));

  //prepare the record to be added to the section by removing the num value and tablename from the array
  unset($oldRecord['_tableName']);
  unset($oldRecord['num']);

  //Insert the record into the new table, the new record number is returned
  $newRecNum = mysql_insert('new_section', $oldRecord);


  //Cycle through the old records uploads
  foreach($oldUploads as $upload){
    
    //Create the file path to the original upload from the CMS SETTINGS array and the uploads file name for each image
    $filePath = $SETTINGS['installPath'].'/'.$SETTINGS['uploadDir'].$upload['filePath'];

    //Use the the CMS save from file path function to copy the image an create a new upload record for it.
    //Variables in order: section name, field name, record num, temp ID (not required here), and file path
    saveUploadFromFilepath('new_section', 'upload', $newRecNum, '', $filePath); 
  }

Hopefully the notes above explain what's happening at each step. All of the functions used above are built into CMS Builder. 

Just to clarify; I'd named the upload field for the section 'upload' in both my sections, and I'm copying a record from old_section to new_section. 

Let me know if you need any help implementing this code.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com