vincej Posted May 10, 2012 Share Posted May 10, 2012 Hi - I have spent 2 days trying to figure this out. I must be doing something stupid ... I am trying to update the DB with a collection of variables off of a various POST array's. I have indexed the variables so that I can loop through them with a FOR. However I am having all kinds of trouble: 1 - WEIGHT is giving 5 values even though the counter should only go 4 times. 2 - The WEIGHT variable is going into the wrong field in the DB even though I specify in my Query a specific place. 3 - I get undefined offset errors even though I have initialised the variables with their respective offsets. 4 - Adding the conditional statement adds even more offset errors even though they are initialised I am starting to wonder if my entire approach to updating my DB is flawed. As a student of PHP I think I need someone with more experience to look at what I am doing and tell me if something jumps out at them. There is a lot of statements echoing out values - and they all seem to check out. Man Many thanks for your advice function finishedorder(){ $prodname = $_POST['prodname']; $prodid =$_POST['prodid']; $quantity = $_POST['quantity']; $pricelb = $_POST['pricelb']; $customerid = $_POST['customerid']; $price = $_POST['price']; $orderid = $_POST['orderid']; $weight = $_POST['weight']; echo "Varuable WEight"; print_r($weight); echo"<br/>"; echo "line 196 Weight"; print_r ($_POST['weight']); echo"<br/>"; echo "line 192 PriceLB"; print_r ($_POST['pricelb']); echo "<br/>"; echo "line 193 Price"; print_r ($_POST['price']); echo "<br/>"; echo "line 194 ProdID"; print_r ($_POST['prodid']); echo "<br/>"; echo "line 195 OrderID"; print_r ($_POST['orderid']); echo "<br/><br/>"; $numloops = count($_POST['prodid']); for ($i = 0; $i < $numloops ; $i++) { // !!! Start of the for loop !!! if (!isset($orderid[$i])) $orderid[$i] =''; if (!isset($prodid[$i])) $prodid[$i] =''; if (!isset($weight[$i])) $weight[$i] =''; if (!isset($pricelb[$i])) $pricelb[$i] =''; // This only initialises the Price & Weight variable to avoid an offset error message. if (!isset($weight[$i])) $weight[$i] =''; $ordervalue = $pricelb[$i] * $weight[$i]; echo " L. 216 The number is " . $i . "<br />"; echo "line 221 ProdID".$prodid[$i];echo "<br/>"; echo "line 217 Pricelb".$pricelb[$i];echo "<br/>"; echo 'Weight L. 219 weight[i]'. $weight[$i];echo "<br/>"; echo "line 220 weight".$weight; echo "<br/>"; echo "line 221 Numloops".$numloops; echo "<br/><br/>"; echo ' L. 225 $ordervalue ' .$ordervalue;echo "<br/>"; } //if ($pricelb[$i] == 0 ){ // conditonal statement // $ordervalue = $price[$i]; // } $sql =" UPDATE confirmedorder SET weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open' WHERE prodID = '$prodid[$i]' AND OrderID = '$orderid[$i]' ; "; $this->db->query($sql); } // !! End For Loop. !! Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/ Share on other sites More sharing options...
batwimp Posted May 10, 2012 Share Posted May 10, 2012 Your array offset $i doesn't exist outside your for loop, so not sure what its value will be in your UPDATE statement. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344610 Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 "Your for loop. I do not think it ends where you think it ends." Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344613 Share on other sites More sharing options...
batwimp Posted May 10, 2012 Share Posted May 10, 2012 Inconceivable! Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344614 Share on other sites More sharing options...
vincej Posted May 10, 2012 Author Share Posted May 10, 2012 Thanks for the feedback : I have checked the ending if the for loop and it does end where it ends. Dreamweaver checks it for you by balancing braces . the Update statement is inside the For loop so it will know what the offset $i is I'm not sure what is implied by 'inconceivable' ? Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344618 Share on other sites More sharing options...
vincej Posted May 10, 2012 Author Share Posted May 10, 2012 I should add that the For loop does Loop - it just is not giving me the results I expect. and for the life of me I can not see what or what to do about it. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344619 Share on other sites More sharing options...
batwimp Posted May 10, 2012 Share Posted May 10, 2012 According to my editing software, the for loop ends before the UPDATE is done. "Inconceivable" is a quote from the same movie xyph was (sort of) quoting. Just for fun. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344621 Share on other sites More sharing options...
vincej Posted May 10, 2012 Author Share Posted May 10, 2012 It's a mystery - I just pulled the code off phpfreaks nad put it back into Dreamweaver and it checks out. No matter - the key issue is trying to find a solution to my other problems, Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344622 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 Assuming that the $_POST['prodid'] count is really WHAT you want to be using for loop ending value, moving the end loop tag so it surrounds the insert should fix the problem. Note: what you had commented as the closing loop was the end of the function loop. <?php function finishedorder(){ $prodname = $_POST['prodname']; $prodid =$_POST['prodid']; $quantity = $_POST['quantity']; $pricelb = $_POST['pricelb']; $customerid = $_POST['customerid']; $price = $_POST['price']; $orderid = $_POST['orderid']; $weight = $_POST['weight']; echo "Varuable WEight"; print_r($weight); echo"<br/>"; echo "line 196 Weight"; print_r ($_POST['weight']); echo"<br/>"; echo "line 192 PriceLB"; print_r ($_POST['pricelb']); echo "<br/>"; echo "line 193 Price"; print_r ($_POST['price']); echo "<br/>"; echo "line 194 ProdID"; print_r ($_POST['prodid']); echo "<br/>"; echo "line 195 OrderID"; print_r ($_POST['orderid']); echo "<br/><br/>"; $numloops = count($_POST['prodid']); for ($i = 0; $i < $numloops ; $i++) { // !!! Start of the for loop !!! if (!isset($orderid[$i])) $orderid[$i] =''; if (!isset($prodid[$i])) $prodid[$i] =''; if (!isset($weight[$i])) $weight[$i] =''; if (!isset($pricelb[$i])) $pricelb[$i] =''; // This only initialises the Price & Weight variable to avoid an offset error message. if (!isset($weight[$i])) $weight[$i] =''; $ordervalue = $pricelb[$i] * $weight[$i]; echo " L. 216 The number is " . $i . "<br />"; echo "line 221 ProdID".$prodid[$i];echo "<br/>"; echo "line 217 Pricelb".$pricelb[$i];echo "<br/>"; echo 'Weight L. 219 weight[i]'. $weight[$i];echo "<br/>"; echo "line 220 weight".$weight; echo "<br/>"; echo "line 221 Numloops".$numloops; echo "<br/><br/>"; echo ' L. 225 $ordervalue ' .$ordervalue;echo "<br/>"; //if ($pricelb[$i] == 0 ){ // conditonal statement // $ordervalue = $price[$i]; // } $sql =" UPDATE confirmedorder SET weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open' WHERE prodID = '$prodid[$i]' AND OrderID = '$orderid[$i]' ; "; $this->db->query($sql); } // !! End For Loop. !! } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344623 Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 If you tabulate your code properly, you will see your issue. I don't care what Dreamweaver says. <?php function finishedorder() { $prodname = $_POST['prodname']; $prodid = $_POST['prodid']; $quantity = $_POST['quantity']; $pricelb = $_POST['pricelb']; $customerid = $_POST['customerid']; $price = $_POST['price']; $orderid = $_POST['orderid']; $weight = $_POST['weight']; echo "Varuable WEight"; print_r($weight); echo"<br/>"; echo "line 196 Weight"; print_r($_POST['weight']); echo"<br/>"; echo "line 192 PriceLB"; print_r($_POST['pricelb']); echo "<br/>"; echo "line 193 Price"; print_r($_POST['price']); echo "<br/>"; echo "line 194 ProdID"; print_r($_POST['prodid']); echo "<br/>"; echo "line 195 OrderID"; print_r($_POST['orderid']); echo "<br/><br/>"; $numloops = count($_POST['prodid']); for ($i = 0; $i < $numloops; $i++) { if (!isset($orderid[$i])) $orderid[$i] = ''; if (!isset($prodid[$i])) $prodid[$i] = ''; if (!isset($weight[$i])) $weight[$i] = ''; if (!isset($pricelb[$i])) $pricelb[$i] = ''; if (!isset($weight[$i])) $weight[$i] = ''; $ordervalue = $pricelb[$i] * $weight[$i]; echo " L. 216 The number is " . $i . "<br />"; echo "line 221 ProdID" . $prodid[$i]; echo "<br/>"; echo "line 217 Pricelb" . $pricelb[$i]; echo "<br/>"; echo 'Weight L. 219 weight[i]' . $weight[$i]; echo "<br/>"; echo "line 220 weight" . $weight; echo "<br/>"; echo "line 221 Numloops" . $numloops; echo "<br/><br/>"; echo ' L. 225 $ordervalue ' . $ordervalue; echo "<br/>"; } // WOA THE FOR ENDS! //if ($pricelb[$i] == 0 ){ // $ordervalue = $price[$i]; //} $sql = " UPDATE confirmedorder SET weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open' WHERE prodID = '$prodid[$i]' AND OrderID = '$orderid[$i]' ; "; $this->db->query($sql); } ?> As a student of PHP, you should uninstall Dreamweaver. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344628 Share on other sites More sharing options...
vincej Posted May 10, 2012 Author Share Posted May 10, 2012 Bugger me - I take it all back. You were right, I was indeed missing a brace at the end !!!! Thank you ! However, it's almost working but not quite. One of the WEIGHT variables is not in the correct field even though I specify a PRODID AND ORDERID. look at Prodid 101. It should say WEIGHT:7 instead the 7 is in the next row. To illustrate the problem here is a print of the table: ID OrderID CustomerID ProdID Prodname Price Quantity Pricelb Weight OrderValue Picking Sale 151 JAC9506 5 17 Ribs 60.00 1 5.00 6.00 30.00 finished open 152 JAC9506 5 28 25 Piece Bag 15.00 1 0.00 0.00 15.00 finished open 153 JAC9506 5 101 breasts 10.25 1 2.00 0.00 0.00 finished open 154 JAC6193 5 27 Sirloin Steak 24.00 1 0.00 7.00 24.00 finished open Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344631 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 That print must be an html table not the DB table. What does the DB table look like when doing a clean update,i.e. closing browser and reloading fresh copy of the page and posting your form again. Does new DB values match? Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344636 Share on other sites More sharing options...
vincej Posted May 10, 2012 Author Share Posted May 10, 2012 It is a cut and paste from MYSQL print preview. I tried closing FF and reopening twice - didn't help. Bummer . Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344638 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 Interesting, so print preview does First Caps on field names. They don't match your insert names. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344641 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 If they need to match that will be me .. I always do first caps on table names. So far I have not seen MySQL ever be case dependant .... mistake ? Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344670 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 What do you see when adding echo sql. Does product 101 have weight of 7? $sql =" UPDATE confirmedorder SET weight='$weight[$i]', ordervalue='$ordervalue', confirmedorder.picking = 'finished', confirmedorder.sale = 'open' WHERE prodID = '$prodid[$i]' AND OrderID = '$orderid[$i]' ; "; echo "$sql<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344673 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 Ok - I ran the test, and the results are consistent with the MYSQL print: UPDATE confirmedorder SET weight='0.00', ordervalue='0', confirmedorder.picking = 'finished', confirmedorder.sale = 'open' WHERE prodID = '101' AND OrderID = 'JAC9506' ; Thanks for sticking with me on this ! btw - how do you get colored code on this site .. mine comes out in bw. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344676 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 Are all your form tags arrays? name='prodname[]' etc. Seems like a simple foreach loop should handle this fine. Btw. code with <?php ?> will have color. ALSO is product id ever duplicated in the form? Just wondering why you have count($_POST['prodid']). OK, I assume this is an array... Never mind. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344678 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 Sorry if I'm not seeing the big picture and how this code fits into what you have going, but it would seem to me that a simple foreach loop would grab the the "post array key" and using this you identify each item and make this update. Again sorry if this is way off. <?php function finishedorder(){ foreach ($_POST['prodid'] as $arrkey => $prodid){ $pricelb = $_POST['pricelb'][$arrkey]; $price = $_POST['price'][$arrkey]; $orderid = $_POST['orderid'][$arrkey]; $weight = $_POST['weight'][$arrkey]; $ordervalue = $pricelb * $weight; $sql =" UPDATE confirmedorder SET weight='$weight', ordervalue='$ordervalue', picking = 'finished', sale = 'open' WHERE prodID = '$prodid' AND OrderID = '$orderid'"; $this->db->query($sql); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344694 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 Hi Drummin - thanks for this idea, also thank you very much for the generosity of your time and spirit. I am always amazed how some people get a kick out of patronizing posters whilst people like you are so generous. Where are you ? I'm in Canada. I would love to change the approach. The reason why I chose a for loop is because my POST arrays come in looking like this: Array ProdID: Array ( [0] => 17 [1] => 28 [2] => 101 [3] => 27 ) a For loop allows me to increment the index like this: $prodid[$i]. If there is a way of incrementing the index, I'm not sure how to do that .. any ideas ? Many thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344798 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 You probably could sort() the list first then do the foreach loop. <?php function finishedorder(){ sort($_POST['prodid']); foreach ($_POST['prodid'] as $arrkey => $prodid){ $pricelb = $_POST['pricelb'][$arrkey]; $price = $_POST['price'][$arrkey]; $orderid = $_POST['orderid'][$arrkey]; $weight = $_POST['weight'][$arrkey]; $ordervalue = $pricelb * $weight; $sql =" UPDATE confirmedorder SET weight='$weight', ordervalue='$ordervalue', picking = 'finished', sale = 'open' WHERE prodID = '$prodid' AND OrderID = '$orderid'"; $this->db->query($sql); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344803 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 Ok - then presumably $arrkey takes care of keeping all the different POST arrays synchronized ? so for example Prodid array([2] => 101) is correctly synchronized with Pricelb array([2] => 101) etc etc etc correct ? Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344805 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 Oops - that should have read: Prodid array([2] => 101) is correctly synchronized with Weight array([2] => 7 )[/size] etc etc etc Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344806 Share on other sites More sharing options...
Drummin Posted May 11, 2012 Share Posted May 11, 2012 Yes you are correct. Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344809 Share on other sites More sharing options...
vincej Posted May 11, 2012 Author Share Posted May 11, 2012 heah Drummin - Thanks again for your code, however I'm not making it work properly. The SQL Query works on the first pass - then it appears to fail without any PHP errors One thing I have noticed is that for some reason the $arrkey and $prodid value is getting messed up when I compare it to the POST ARRAY. Take for example the second UPDATE. $prodid 27 has an $arrkey of 2 yet when you look into the POST it has an array key of [3] This might be causing the SQL query to fail ? NB - I have attached a PNG off the table so you can see how it is failing. MANY THANKS ! line 196 WeightArray ( [0] => 6 [1] => 0.00 [2] => 0.00 [3] => 7 [4] => 0.00 [5] => 0.00 ) line 192 PriceLBArray ( [0] => 5.00 [1] => 0.00 [2] => 2.00 [3] => 0.00 ) line 193 PriceArray ( [0] => 60.00 [1] => 15.00 [2] => 10.25 [3] => 24.00 ) line 194 ProdIDArray ( [0] => 17 [1] => 28 [2] => 101 [3] => 27 ) line 195 OrderIDArray ( [0] => JAC9506 [1] => JAC9506 [2] => JAC9506 [3] => JAC6193 ) UPDATE confirmedorder SET weight='6', ordervalue='30', picking = 'finished', sale = 'open' WHERE prodID = '17' AND OrderID = 'JAC9506' arrkey0 prodid 17 Pricelb5.00 UPDATE confirmedorder SET weight='0.00', ordervalue='0', picking = 'finished', sale = 'open' WHERE prodID = '27' AND OrderID = 'JAC9506' arrkey1 prodid 27 Pricelb0.00 UPDATE confirmedorder SET weight='0.00', ordervalue='0', picking = 'finished', sale = 'open' WHERE prodID = '28' AND OrderID = 'JAC9506' arrkey2 prodid 28 Pricelb2.00 UPDATE confirmedorder SET weight='7', ordervalue='0', picking = 'finished', sale = 'open' WHERE prodID = '101' AND OrderID = 'JAC6193' arrkey3 prodid 101 Pricelb0.00 Quote Link to comment https://forums.phpfreaks.com/topic/262376-why-insert-fail-when-i-put-an-if-inside-a-for-loop/#findComment-1344828 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.