Unknown file id 0 in table files
Unknown file id 0 in table files
itxuser
Posts: 18Questions: 5Answers: 0
Getting this error, see attached, when going to edit/add an image using the upload library. I am getting a null in my both of my tables when performing this operation. The only fields getting populated are: filename
and filesize
. I am attaching my code for review also to see if anyone sees my issue. Thanks:
parts.php
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "partsData.php",
table: "#example",
fields: [
{
label: "Category:",
name: "TBL_NAME.Category"
}, {
label: "Description:",
name: "TBL_NAME.PartDescription"
}, {
label: "Details:",
type: "textarea",
name: "TBL_NAME.PartDetails"
}, {
label: "Cost:",
name: "TBL_NAME.Cost"
}, {
label: "List Price:",
name: "TBL_NAME.ListPrice"
}, {
label: "UOM:",
name: "TBL_NAME.UOM"
}, {
label: "Height:",
name: "TBL_NAME.Height"
}, {
label: "Width:",
name: "TBL_NAME.Width"
}, {
label: "Length:",
name: "TBL_NAME.Length"
}, {
label: "Weight:",
name: "TBL_NAME.Weight"
}, {
label: "Image:",
name: "TBL_NAME.image",
type: "upload",
display: function ( id ) {
return '<img src="'+editor.file( 'files', id ).web_path+'"/>';
},
clearText: "Clear",
noImageText: 'No image'
}
]
} );
// Activate an inline edit on click of a table cell
$('#example').on( 'click', 'tbody td.editable', function (e) {
editor.inline( this );
} );
var table = $('#example').DataTable( {
lengthChange: false,
ajax: {
url: "partsData.php",
type: "POST"
},
serverSide: true,
columns: [
{ data: "TBL_NAME.PartNumber"},
{ data: "TBL_NAME.PartDescription", className: 'editable' },
{ data: "TBL_NAME.PartDetails", className: 'editable' },
{ data: "TBL_NAME.UPC"},
{ data: 'TBL_NAME.image', render: function ( id ) {
return id ?
'<img class="img-responsive" src="'+editor.file( 'files', id ).web_path+'" />' :
'No image';
} },
],
select: true
} );
// Display the buttons
new $.fn.dataTable.Buttons( table, [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
] );
table.buttons().container()
.appendTo( $('.col-sm-6:eq(0)', table.table().container() ) );
} );
partsData.php
<?php
include("php/DataTables.php");
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst($db, 'TBL_NAME')
->fields(
Field::inst( 'TBL_NAME.Category' ),
Field::inst( 'TBL_NAME.PartNumber' ),
Field::inst( 'TBL_NAME.PartDescription' ),
Field::inst( 'TBL_NAME.PartDetails' ),
Field::inst( 'TBL_NAME.Cost' ),
Field::inst( 'TBL_NAME.ListPrice' ),
Field::inst( 'TBL_NAME.UOM' ),
Field::inst( 'TBL_NAME.UPC' ),
Field::inst( 'TBL_NAME.Weight' ),
Field::inst( 'TBL_NAME.Width' ),
Field::inst( 'TBL_NAME.Height' ),
Field::inst( 'TBL_NAME.Length' ),
Field::inst( 'files.id' ),
Field::inst( 'files.filename' ),
Field::inst( 'TBL_NAME.image' )
->setFormatter( 'Format::ifEmpty', null )
->upload( Upload::inst( '/var/www/html/media/images/__ID__.__EXT__' )
->db( 'files', 'id', array(
'filename' => Upload::DB_FILE_NAME,
'filesize' => Upload::DB_FILE_SIZE,
'web_path' => Upload::DB_WEB_PATH,
'system_path' => Upload::DB_SYSTEM_PATH
) )
->allowedExtensions( array( 'png', 'jpg', 'gif' ), "Please upload an image" )
)
)
->leftJoin( 'files', 'TBL_NAME.image', '=', 'files.id' )
->process($_POST)
->json();
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
forgot to attach image of error:
When you upload the file, what is the JSON that is being returned by the server?
Thanks,
Allan
Here is the returned json.
i removed my joined table formatting on partsData.php as I don't think I need it
Thanks for the JSON data. That shows why it is requesting information about file id 0, the server is telling the client-side that that is the id of the file that was uploaded.
Can you show me the schema of your
files
table from the database please?Thanks,
Allan
I finally figured it out. I didn't have the
id
column configured correctly in myfiles
table as a primary/unique structure that was related to the other table. I fixed that part and now it is all working as expected. Thank you for the help!