Thursday 15 April 2010

php - Undefined Offset: 0 - Codeigniter -


When I send a request to this page with post-data ({"bot_hw_id": "2147483647"}) I get the following error:

  & lt; P & gt; Seriousness: notice & lt; / P & gt; & Lt; P & gt; Message: Undefined Offset: 0 & lt; / P & gt; & Lt; P & gt; Filename: Model / Promet_model.fp & lt; / P & gt; & Lt; P & gt; Line number: 26 & lt; / P & gt;   

My code:

  Controller (update_bot function): [code] Public function update_bot () {$ bot_data = json_decode ($ this-> Input - & gt; Post ('bot_data')); $ To_update = array ('bot_last_update' = & gt; time (), 'bot_ip' = & gt; $ _SERVER ['REMOTE_ADDR'], 'bot_port' = & gt; $ _SERVER ['REMOTE_PORT']); $ Bot_data = $ this- & gt; Prometheus_model- & gt; Get_bot_data_by_hw_id (/ * $ bot_data- & gt; {'bot_hw_id'} * / $ bot_data- & gt; {'bot_hw_id'}); // echo $ bot_data ['bot_id']; Print_r (bot_data $); $ This- & gt; Prometheus_model- & gt; Update ('bots', array ('bot_id' = & gt; $ bot_data ['bot_id']), $ to_update; // var_dump ($ bot_data); }   

Model (Promet_model):

  & lt ;? Php class extends prometheus_model cImodel {var $ tables = array ('bots' = & gt; bots'); Function __ composition () {origin: __ creation (); } Public function tablename ($ table = NULL) {if (! Isset ($ table)) FALSE return; Return $ this- & gt; Tables [$ table]; } Get the public function ($ table, $ where = array), $ single = wrong, $ order = zero) {$ this- & gt; Db- & gt; Where ($ where); If (isset ($ command)) {$ this-> Db- & gt; Order_b ($ command); } $ Q = $ this- & gt; Db- & gt; Get_where ($ this-> tablain ($ table), $ where); $ Result = $ q- & gt; Result_are (); If ($ single) {Return of $ return [0]; } Return result; } Public Function Updates ($ table, $ where = array (), $ data) {$ this- & gt; Db- & gt; Update ($ - $ -> Tableline ($ table), $ data, $ where); Return $ $-> Db- & gt; Affected_ro (); } Enter public function ($ table, $ data) {$ this- & gt; Db- & gt; Insert ($ -> - $ tablename ($ table), $ data); Return $ the-> gt; & Gt; Insert_id (); } Delete Public Function ($ table, $ where = array)) {$ this- & gt; Db- & gt; Delete ($ this- & gt; tablename ($ table), $ where); Return $ $-> Db- & gt; Affected_ro (); } Public Work Explicit ($ query) {$ q = $ this- & gt; Db- & gt; Query ($ query); If (is_object ($ q)) {return $ q- & gt; Result_are (); } And {return $ q; }} Public function num_rows ($ table, $ where = faucet) {if (isset ($ where)) {$ this- & gt; Db- & gt; Where ($ where); } $ Q = $ this- & gt; Db- & gt; Get ($ table); Return $ q- & gt; Num_rows (); } Public function get_bot_data_by_hw_id ($ bot_hw_id) {$ q = $ this- & gt; Obtain ('bots', array ('bot_hw_id' =>; bot_hw_id;);); Return $ q; }}? & Gt ;;   

How can I fix this error?

First of all, I should mention that this is a good and safe Not to think, to query user input without any process (as we see in your code)

Anyway ...

Your function will be corrected as follows:

  public function ($ table, $ where = array () , $ Single = wrong, $ order = zero) Get {$ this- & gt; Db- & gt; Where ($ where); If (isset ($ command)) {$ this-> Db- & gt; Order_b ($ command); } $ Q = $ this- & gt; Db- & gt; Get_where ($ this-> tablain ($ table), $ where); $ Result = $ q- & gt; Result_are (); // If you want to find the number of rows returned, type $ q-> Num_rows () should be used ($ q-> num_rows () == 1) {// Return to the first line: the result of the return [0]; } Return result; }   

This returns the first line when there is only one, and when a code is $ q-> Num_rows () is not equal to , 1 .

Hope this helps

No comments:

Post a Comment