Forums

Re-upload file and update record

rmesman 16 May, 2009
Hi,
I need a solution to be able to re-upload a file after it has been submitted once.

This is my case:
I have a form where people can apply for jobs. In this form they upload a file (e.g. MsWord doc) which is being stored. They need to be able to replace the file.
I tried with CC (ChronoConnectivity), but nothing happens when I try to edit the record and upload a file again. Also CC doesn't handle the file-location.

I also tried making a seperate Form in CF (ChronoForms) which gets a variable to indentify the record which has to be updated and enables to upload a new letter:
Form code:
<?php
    $v_vacnr = &JRequest::getVar('vac_nr', '', 'GET'); 
    $db =&JFactory::getDBO();
    $query = "SELECT * FROM jos_chronoforms_lincavac 
        WHERE vac_nr = '$v_vacnr'";
    $db->setQuery($query);
    $rows = $db->loadObject();
?>

<table>
<tr>  /*show if it is the right record we are updating*/
<td>Vacature nummer</td><td><?php echo "$rows->vac_nr" ?></td>
</tr>
<tr>
<td>Upload Vacature-PDF/Doc</td>
<td ><input name="vac_pdf" id="vac_pdf" type="file" /></td>
</tr>
</table> 
<button type="submit" >verstuur</button>


2 problems:
1.No file is being upload (although all form-settings for file-upload are ok), but is a minor problem right now..
2.Main problem is that also a new record is being made in the DB. And this I don't want. I only want to update the record-field 'vac_pdf' and upload a new file.


So I think my solution would be to make a simple php-form myself just for uploading a file and make a script in which the name of the file (which is being saved in the record the first time) is being updated with the new name.
Form code:
<?php
    $v_vacnr = &JRequest::getVar('vac_nr', '', 'GET'); 
    $db =&JFactory::getDBO();
    $query = "SELECT * FROM jos_chronoforms_lincavac 
        WHERE vac_nr = '$v_vacnr'";
    $db->setQuery($query);
    $rows = $db->loadObject();
?> 
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
New file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


Where upload.php contains:
$target_path = "../uploads/"; /* define the target path her*/
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); /* store file */
...some code to replace field 'vac_pdf' with '$_FILES['uploadedfile']['name']'......


Is there a way to do this, in a simple way, with CF?

Thnx,Robin
GreyHead 16 May, 2009
Hi remesman,

If you add a hidden field to your form with the table primary key value in it (presumably vac_nr) then the existing record should be updated instead of a new one being added.
<input type='hidden' name='vac_nr' value='<?php echo $v_vacnr; ?>' />

Bob
rmesman 16 May, 2009
Hi Bob,

In what form do you want me to put this hidden field? The CF form or my own form?

Robin
GreyHead 16 May, 2009
Hi Robin,

In the ChronoForms Form which you say isn't Updating correctly.

Bob
rmesman 17 May, 2009
Hi Bob,

The primary key for all CF forms is "cf_id" so I added the following code to the form:
<input type="hidden" name="cf_id" value="<?php echo '$rows->cf_id' ?>" />
<tr> /*to verify the correct cf_id is being displayed*/
<td>CF_ID</td><td><?php echo "$rows->cf_id" ?></td>
</tr>


I've put debug 'on' and this is the result after submitting:
[attachment=0]debug.png[/attachment]

So it seems everything is ok, but no file is being uploaded and also 'vac_pdf' isn't being updated.
Any ideas?

Robin
rmesman 17 May, 2009
Ok, I was a bit to quick with the results ...

It did upload the file, so that was covered, but it didn't update 'vac_pdf' with the new filename?

To make sure I update the right record I changed the hidden field to a non-hidden input:

<tr>
<td >CF_ID</td><td >
<input name="cf_id" id="cf_id" value="<?php echo '$rows->cf_id' ?>" />
</td>
</tr>


when opening the form it showed the string '$rows->cf_id' in stead of the correct cf_id-number. So I removed the '' around $rows->cf_id:

<input name="cf_id" id="cf_id" value="<?php echo $rows->cf_id ?>" />


Now it works!
Thanks for your help

Robin
GreyHead 18 May, 2009
Hi Robin.

Great. Glad it's working OK.

Quotes are pesky!!

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