а

Problem in inserting data to the database


#1

The insert, delete and select of my codes are fine, it’s just that when i add 1 record it duplicates making it 2 records at the same time to the database. I have the correct codes i guess. Can someone help me?

Kindly go to if (id==“bank”)
Here is my code:

[code]

SMCT Group of Companies html, body { width: 100%; height: 100%; margin: 0px; overflow: hidden; background-color:white; }
	<script type="text/javascript">
		dhtmlxEvent(window,"load",function(){ 
		var layout = new dhtmlXLayoutObject(document.body,"1C");
		var a = layout.cells('a');
		a.hideHeader();
		var form_1 = a.attachForm();
		var toolbar1 = layout.attachToolbar();
		toolbar1.setIconSize(28);
		toolbar1.setIconsPath("icons/");
		toolbar1.loadStruct("data/dhxtoolbar_button2");
		var menu_1 = layout.attachMenu();
		menu_1.setSkin("dhx_skyblue");
		menu_1.setIconsPath('icons/');
		menu_1.loadStruct('data/dhxmenu.xml');
		menu_1.attachEvent("onClick", function(id, zoneId, cas)
	{

// Start of Sales onClick Event
if (id == “sales”){
{
var grid_1 = a.attachGrid();
grid_1.attachEvent(“onXLS”,function(){
layout.cells(“a”).progressOn();
})
grid_1.setSkin(“dhx_skyblue”);
a.showHeader();
a.setText(‘Sales’);
grid_1.setIconsPath(‘icons/’);
grid_1.setHeader([“Code”,“Product”]);
grid_1.setColTypes(“ro,ro”);
grid_1.setColSorting(‘str,str’);
grid_1.attachHeader("#text_search,#text_search");
grid_1.setInitWidths(’,’);
grid_1.enableSmartRendering(true);
grid_1.load(‘database/categories.php’);
grid_1.attachEvent(“onXLE”,function(){
layout.cells(“a”).progressOff();
})
grid_1.init();
return;
}
}

if (id == "charge"){
	a.setText('Charge');
	var grid_2 = a.attachGrid();	
grid_2.attachEvent("onXLS",function(){
layout.cells("a").progressOn();

})
grid_2.setSkin(“dhx_skyblue”);
grid_2.setIconsPath(‘icons/’);
grid_2.setHeader([“Barcode”,“Product Code”,“Description”,“Category”,“Subcategory”,“Brand”,“Model”,“Supplier”,“Date”]);
grid_2.setColTypes(“ro,ro,ro,ro,ro,ro,ro,ro,ro”);
grid_2.setColSorting(‘str,str,str,int,int,int,str,int,date’);
grid_2.attachHeader("#text_search,#text_search,#text_search,#text_search,#text_search,#text_search,#text_search,#text_search,#text_search");
grid_2.setInitWidths(’,,,,,,,,*’);
grid_2.enableSmartRendering(true);
grid_2.load(‘database/spareparts.php’);
grid_2.attachEvent(“onXLE”,function(){
layout.cells(“a”).progressOff();
})
grid_2.init();
toolbar1.attachEvent(“onClick”, function(id){
});
}
if (id==“bank”){

// A Grid
var inner = a.attachLayout(“2U”);
inner.cells(“a”).attachHTMLString("");
inner.cells(“a”).setWidth(350);
inner.cells(“a”).setText(“Bank Form”);
inner.cells(“a”).hideHeader();
inner.cells(“b”).attachHTMLString(“2U right”);
inner.cells(“b”).setText(“Bank Records”);
var Grid1 = inner.cells(“b”).attachGrid();
Grid1.setHeader(" Code,Branch Name,Account Title,Address,Manager Name,Account Number");
Grid1.setColumnIds(“branch_id,bank_name,contact_person,address,manager_name,account_number”);
Grid1.setInitWidths(“70,150,250,,,*”);
Grid1.setColAlign(“left,left,left,left,left,left”);
Grid1.setColTypes(“ro,ro,ro,ro,ro,ro”);
Grid1.setColSorting(“str,str,str,str”);
Grid1.attachHeader("#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter");
Grid1.init();
Grid1.load(“database/bank.php”);
contactForm = inner.cells(“a”).attachForm();
contactForm.loadStruct(“data/bank.xml”);
contactForm.bind(Grid1);

				  var dpg = new dataProcessor("database/bank.php");
				 dpg.init(Grid1);
				dpg.attachEvent("onAfterUpdate", function(sid, action, tid, tag){
			   if (action == "inserted"){
			  Grid1.selectRowById(tid);
			 contactForm.setFocusOnFirstActive();
}

})
contactForm.attachEvent(“onButtonClick”, function(id){
contactForm.save();
});
toolbar1.attachEvent(“onclick”,function(id){
if(id==“add”){
dhtmlx.message({
text:“New Row Successfully Added!”,
expire:4000,
type:“customCss” // ‘customCss’ - css class
});
var rowId=Grid1.uid();
var pos = Grid1.getRowsNum();
Grid1.addRow(rowId,["","",""],pos);
};
if(id==“delete”){
dhtmlx.message({
text:“Record Successfully Deleted!”,
expire:4000,
type:“customCss” // ‘customCss’ - css class
});
var rowId = Grid1.getSelectedRowId();
var rowIndex = Grid1.getRowIndex(rowId);
if(rowId!=null){
Grid1.deleteRow(rowId);
if(rowIndex!=(Grid1.getRowsNum()-1)){
Grid1.selectRow(rowIndex+1,true);
}
else{
Grid1.selectRow(rowIndex-1,true)
}
}
}
});
}

if (id==“areas”){

// A Grid
var inner = a.attachLayout(“2U”);
inner.cells(“a”).attachHTMLString("");
inner.cells(“a”).setWidth(350);
inner.cells(“a”).hideHeader();
inner.cells(“b”).attachHTMLString(“2U right”);
inner.cells(“b”).setText(“Area Records”);
var Grid1 = inner.cells(“b”).attachGrid();
Grid1.setHeader(“Area,Description,Date”);
Grid1.setColumnIds(“area_name,description,date”);
Grid1.setInitWidths(“300,300,*”);
Grid1.setColAlign(“left,left,left”);
Grid1.setColTypes(“ro,ro,ro”);
Grid1.setColSorting(“str,str,str”);
Grid1.attachHeader("#text_filter,#text_filter,#text_filter");
Grid1.init();
Grid1.load(“database/areas.php”);
contactForm = inner.cells(“a”).attachForm();
contactForm.loadStruct(“data/areas.xml”);
contactForm.bind(Grid1);

				  var dpg = new dataProcessor("database/areas.php");
				 dpg.init(Grid1);
				dpg.attachEvent("onAfterUpdate", function(sid, action, tid, tag){
			   if (action == "inserted"){
			  Grid1.selectRowById(tid);
			 contactForm.setFocusOnFirstActive();
}

})
contactForm.attachEvent(“onButtonClick”, function(id){
contactForm.save();
});
toolbar1.attachEvent(“onclick”,function(id){
if(id==“add”){
dhtmlx.message({
text:“New Row Successfully Added!”,
expire:4000,
type:“customCss” // ‘customCss’ - css class
});
var rowId=Grid1.uid();
var pos = Grid1.getRowsNum();
Grid1.addRow(rowId,["","",""],pos);
};
if(id==“delete”){
dhtmlx.message({
text:“Record Successfully Deleted!”,
expire:4000,
type:“customCss” // ‘customCss’ - css class
});
var rowId = Grid1.getSelectedRowId();
var rowIndex = Grid1.getRowIndex(rowId);
if(rowId!=null){
Grid1.deleteRow(rowId);
if(rowIndex!=(Grid1.getRowsNum()-1)){
Grid1.selectRow(rowIndex+1,true);
return 0;
}
else{
Grid1.selectRow(rowIndex-1,true)
return 0;
}
}
}
});
}
});
})

[/code]


#2

a) try to move the code that inits dataprocessor outside of onload handler, to ensure that dataprocessor is initialized only once. If dataprocessor will be initialized twice, it will send two requests and will create multiple records in the database.

b) double check the server side response, it must be in the valid format with all necessary attributes. If server-side response is broken, client side will not confirm data inserting operation and will try to issue the same operation again, which may lead to the adding two records to a database.


#3

hi stan! glad for your reply, unfortunately i tried to move it outside the handler but still it’s the same. I also checked server side’s response, it’s all good and working. I am really having a hard time configuring this out.


#4

I don’t see any other reason why the record is inserted twice in the database.
Can you please try to add some console.log statement next to the dataprocessor init and check that is executed only once.

if you are using latest version of DHTMLX, you can try to add dp.enableDebug(true) to get some more info about data saving process on a client side. There is a way to enable logs on server side as well. docs.dhtmlx.com/connector__php__errors.html


#5

Thanks all for your responses. i was having similar questions and your answers were helpful to me.