0

I´m using CodeIgniter with jquery grid. I have an Index in my DB that states that some combination can´t be inserted. UNIQUE KEY user_store_index (Stores_idStores,Users_idUsers). The combination of a user and a Store can´t be inserted twice. Using firebux I can see the error A Database Error Occurred Error Number: 1062. Duplicate entry '1-5' for key 'user_store_index' INSERT INTO asignedto (Stores_idStores, Users_idUsers) VALUES ('1', '5').

In this case I would like to show to the user in the jqGrid the following message: "You can´t insert this combination because it already exists" but all I got is "error Status: 'error'. Error code: 500". I have read the explanation presented here jqgrid server side error message/validation handling and here jqgrid server exception error messages and many others but I haven´t been able to find the solution.

Here is the code of my jqGrid

  $(document).ready(function() {

           var grid = jQuery("#newapi_1351802691").jqGrid({
                    ajaxGridOptions : {type:"POST"},
                       jsonReader : {
                       root:"data",
                       repeatitems: false
                   },
                   loadError : function(xhr,status,error) {
                            alert(xhr.status+','+status+','+error);  //alert 500, error, My custom error
                    },
                    rowList:[10,20,30],
                    viewrecords: true
                   ,url:'http://localhost/sp/index.php/assigment/getData'
,editurl:'http://localhost/sp/index.php/assigment/setData'
,datatype:'json'
,rowNum:'12'
,width:'800'
,height:'300'
,pager: '#pnewapi_1351802691'
,caption:'Control de asignaciones'
,colModel:[
{name:'username',index:'username',label:'Usuario' ,align:'left',width:300,editable:false,edittype:'text',editrules:{required:true}  }
,{name:'StoreName',index:'StoreName',label:'Tienda' ,align:'center',width:200,editable:false,edittype:'text',editrules:{required:true}  }
,{name:'Users_idUsers',index:'Users_idUsers',label:'Usuario' ,align:'center',width:100,hidden:true,editable:true,edittype:'select',editoptions:{value:":Select;5:tienda1;6:tienda2;17:tienda3;18:tienda11",size:10},editrules:{edithidden:true,required:true,integer:true}  }
,{name:'Stores_idStores',index:'Stores_idStores',label:'Tienda' ,align:'center',width:100,hidden:true,editable:true,edittype:'select',editoptions:{value:":Select;1:EA001;3:EA003;4:EA005;5:EA006;6:EA007;7:EA008;8:EA009;9:EA010;10:EA011;11:EA012;12:EA013;13:EA015;14:EA017;15:EA018;16:EA019;17:EA020;18:EA021;19:EA022;20:EA002;21:EA000",size:10},editrules:{edithidden:true,required:true,integer:true}  }
,{name:'SurrugateTurn',index:'SurrugateTurn',label:'SurrugateTurn' ,align:'center',width:100,key:true,hidden:true,editable:true,edittype:'select',editoptions:{value:":Select;1:1;19:19",size:10},editrules:{hidden:true}  }
]               })
              jQuery("#newapi_1351802691")
            .jqGrid('navGrid',
            '#pnewapi_1351802691',
            {view:false,
             edit:'1',
             add:'1',
             del:'1',
             close:true,
             delfunc: null ,
             search:''
        },
        { recreateForm: true, 
        width : 400 ,
        beforeShowForm : function(form) {

         },
        closeAfterEdit:true }, // edit options

        {  recreateForm: true,
        width : 400,
         closeAfterAdd : true,
         beforeSubmit : function(postdata, formid) {
         var mensaje = '';
         var resultado = true;




          return[resultado,mensaje]; 
        }
        }, /*add options*/

        {mtype: 'POST'} /*delete options*/,
        {sopt: ['eq','cn','ge','le' ] ,     
        multipleSearch: false ,
        showOnLoad : false,
        overlay:false,mtype: 'POST'} /*search options*/
        ).navButtonAdd('#pnewapi_1351802691',
                { caption:'', buttonicon:'ui-icon-extlink', onClickButton:dtgOpenExportdata, position: 'last', title:'Export data', cursor: 'pointer'}
                );
;
        });

Could you please help me with this...

Community
  • 1
  • 1
Caleb
  • 179
  • 2
  • 17
  • The error is being generated on the server, I suggest handling it on the server rather than exposing that error information to your users. You really don't want to expose your table names to malicious users. – Kevin B Nov 01 '12 at 20:09
  • Yes your rigth but I will not show my tables name. I will get the error number from mysql and according to that I will show some personalized message. But the point is the same, I haven't been able to show any error message but the error status 'error'. error code 500 – Caleb Nov 02 '12 at 13:58
  • It's not a matter of you showing it, if your server is returning it, anyone can see it. – Kevin B Nov 02 '12 at 14:12
  • Ok, I will follow your suggestion. I can easily check, before insert if any combination is already in the database but my problem remains!! ..How can I show the error message in jqGrid? – Caleb Nov 02 '12 at 15:30

1 Answers1

0

you better dont use this kind of error handling

reasons:

  • "500 error" you see is CI responding with DB error and returning "500 Internal server error" http header
  • this is core function and displayed automatically, you cannot change it unless you edit the core mysql library.

I suggest to check the uniqueness of the record each time user try to add. And return certain error to handle it in jqgrid

Serg
  • 606
  • 3
  • 10
  • In CI if I put the db_debug to false I can handle my errors even get the error number returned by mysql but my biggest problems is not that, my problem is How to display any error message in jquery Grid. I have tried with afterSubmit event, with errorTextFormat and nothing. Anyway thanks for your response. – Caleb Nov 01 '12 at 20:06
  • is this http://www.trirand.com/blog/phpjqgrid/examples/editing_data/showerrors/default.php what you looking for? – Serg Nov 02 '12 at 16:04
  • Serg it seems to be what I´m looking for but do you know where can I get the example files? – Caleb Nov 07 '12 at 20:51
  • Isn't it example?? Include jqGrid PHP library and give some try. Froom the example page I see it is returning header 500 state and simply text error. So on client it shows that error. – Serg Nov 08 '12 at 19:55
  • Yes, is an example but I´t uses a non free class jqGrid PHP library. Thanks a lot for your help. – Caleb Nov 08 '12 at 21:35