John Davidson

php - Editing Already Added Documents

0 comments
Message:


I have created this code to edit already added documents on my website.


The problem I have is that the document gets edited in the table but once it has been edited it creates a new duplicate of the document that was edited.


<script>
var documentTable = null;

$(document).ready(function () {
documentTable = $('#documentTable').DataTable({
dom: '<"row"<"col-md-6"B><"col-md-4"f><"col-md-2"l>><rtip>',
ajax: '{{ baseUrl }}/users/api/{{ session.client.id }}/documents',
columns: [
{"data": "id"},
{"data": "name"},
{"data": "description"},
{"data": "type", render: function ( data, type, row ) {
return data.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}},
{
"mRender": function (data, type, row) {
let html = '';
//loadForm parameters: api to retrieve modal form, target div id to load form to, row data, title of
html += '<button class="btn btn-primary small-button" onclick="showForm(\'edit\',\'{{ baseUrl }}/users/api/{{ session.client.id }}/documents/' + row.id + '\')">Edit</button>';
return html;

},
"sClass": "text-right",
"searchable": false
}

],
processing: true,
serverSide: true,
responsive: true,
buttons: [
{
text: 'Create',
action: function (e, dt, node, config) {
let data = {};
showForm('create', '{{ baseUrl }}/users/api/{{ session.client.id }}/documents/form');
}
}

]
});
});
</script>
<div id="form"></div>
<div id="message"></div>
</div>

Here is the function in PhP:


public function updateClientDocument($document, $documentName = null, $documentTmpName = null)
{
if (count($_FILES) == 0) {
return;
}

if (!isset($documentName, $documentTmpName)) {
$documentName = $_FILES["document"]["name"];
$documentTmpName = $_FILES["document"]["tmp_name"];
}

$mainDestinationFolder = UPLOAD_FOLDER;
if ($document->file != '') {
//delete old file
unlink($mainDestinationFolder . "/client/" . $document->clientId . "/" . $document->type . "/" . $document->file);
}

$destinationFolder = $mainDestinationFolder . "/client/" . $document->clientId . "/" . $document->type;
if (!file_exists($destinationFolder)) {
mkdir($destinationFolder, 0700, true);
}
$fileExt = pathinfo($documentName, PATHINFO_EXTENSION);
$fileName = time() . rand(1, 10000) . "." . $fileExt;
$destinationFile = $destinationFolder . "/" . $fileName;
move_uploaded_file($documentTmpName, $destinationFile);

$document->file = $fileName;
$document->save();
}

Any advice on what I can do to be able to edit the documents but not make a duplicated after.



My Answer:
Here is a possible solution to prevent creating a duplicate document when editing:

php
// Check if form is submitted
if(isset($_POST['submit'])){
// Get form data
$id = $_POST['id'];
$newDocument = $_POST['document'];

// Update document in database
// Replace this with your database update query
// For example: $query = "UPDATE documents SET document = '$newDocument' WHERE id = $id";

// Redirect to the same page to prevent form resubmission
header("Location: ".$_SERVER['PHP_SELF']);
exit();
}

// Display documents from database
// Replace this with your database query to fetch documents
// For example: $query = "SELECT * FROM documents";
// Loop through fetched documents and display them in a table
?>











ID Document Action









In this code snippet, we are updating the document in the database when the form is submitted. After updating the document, we redirect back to the same page to prevent form resubmission. This should prevent creating a duplicate document when editing.

Rate this post

4 of 5 based on 5578 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us