mynameisjayson Posted October 31, 2014 Share Posted October 31, 2014 Hi I am having undefined offset:2 and any body can give me an idea, why i get this error message. for($i = 0; $i< count($from_branch) || $i< count($to_branch) || $i< count($quantity) || $i< count($prio_color); $i++) { $query = mysql_query("INSERT INTO pull_out(`description`,`barcode`,`from`,`to`,quantity,prio_color,date_created,remarks,userid) VALUES('$desc','$barc','$from_branch[$i]','$to_branch[$i]','$quantity[$i]','$prio_color[$i]','$datec','$remar','$userid')"); } and i use checkbox under prio_color and i think this is the cause, Quote Link to comment https://forums.phpfreaks.com/topic/292184-undefined-offset-2/ Share on other sites More sharing options...
tryingtolearn Posted October 31, 2014 Share Posted October 31, 2014 The undefined offset means you are referring to an array key that doesn't exist. So one or more of the following are not in your array $from_branch[$i] $to_branch[$i] $quantity[$i] $prio_color[$i] Quote Link to comment https://forums.phpfreaks.com/topic/292184-undefined-offset-2/#findComment-1495344 Share on other sites More sharing options...
tryingtolearn Posted October 31, 2014 Share Posted October 31, 2014 say for example your arrays were as follows $from_branch = array(1,2,3); $to_branch = array(1,2,3,4); $quantity = array(1,2,3,4,5); $prio_color = array(1,2,3,4,5,6); then with your loop $i would be 6 but none of your arrays would have an index of 6 hence the undefined offset. Quote Link to comment https://forums.phpfreaks.com/topic/292184-undefined-offset-2/#findComment-1495345 Share on other sites More sharing options...
mynameisjayson Posted October 31, 2014 Author Share Posted October 31, 2014 so, do you have any idea how, to resolve this.. actually its work to insert but the problem is the prio_color is not working properly, i mean is not inserting in a correct row. Quote Link to comment https://forums.phpfreaks.com/topic/292184-undefined-offset-2/#findComment-1495346 Share on other sites More sharing options...
mynameisjayson Posted October 31, 2014 Author Share Posted October 31, 2014 but i have a correct index from each you suggest. <script type="text/javascript"> var rowNum = 0; function addRow(frm) { rowNum ++; var row = '<p id="rowNum'+rowNum+'"><input type="text" class="form-control" name = "from_branch[]"style = "width:250px;" placeholder="From" value="" required> <input type="text" id="from_2" class="form-control" placeholder="To" style = "width:250px;margin-top:-34px; margin-left:280px;" name = "to_branch[]" value="" required> <input type="text" class="form-control" placeholder="#"style = "width:80px;margin-top:-34px; margin-left:560px;" name = "quantity[]" value="" required> <input type = "checkbox" name = "prio_color[]" value = "#d6f4ca#fd3236" style = "position:absolute;width:80px; margin-top:-23px; margin-left:620px;"> <button type="button" value="Remove" class="btn btn-default" style = "width:40px; margin-top:-57px; margin-left:680px;"onclick="removeRow('+rowNum+');"><span class = "glyphicon glyphicon-minus"></button></p>'; jQuery('#itemRows').append(row); } function removeRow(rnum) { jQuery('#rowNum'+rnum).remove(); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/292184-undefined-offset-2/#findComment-1495348 Share on other sites More sharing options...
tryingtolearn Posted October 31, 2014 Share Posted October 31, 2014 If prio_color isnt inserting then it means it doesn't have that index you are establishing with your loop. so you have to either get the array to have that index or change the way you are inserting it. Hard to come up with a solution not knowing what you are trying to accomplish or how you are getting to this point. Are you just simply trying to add the form results to the DB or something different. Quote Link to comment https://forums.phpfreaks.com/topic/292184-undefined-offset-2/#findComment-1495349 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.