denoteone Posted March 23, 2010 Share Posted March 23, 2010 I am trying to loop through an array and update a database with a $post variable. I am pretty sure it has something to do with the way i am referencing the post variable... maybe I am missing a quote or I have to many in there any help would be awesome. if(isset($_POST['submit3'])){ for($i=0;$i<23;$i++){ $sql="UPDATE price2 SET price='".$_POST[$products1[$i]]."' WHERE storeID='".$_POST['storeid']."' AND storeType = '".$_POST['storetype']."'"; $result = $db->query($sql); echo "success"; } } Quote Link to comment https://forums.phpfreaks.com/topic/196260-using-_post-variable-in-update-string/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 23, 2010 Share Posted March 23, 2010 There's nothing technically wrong with the posted code, provided that you have an array named $products1 with indexes 0 - 22 and values that match the post index names. It would take some point of reference, such as your form and your definition of the $products1 array to be able to help you. Quote Link to comment https://forums.phpfreaks.com/topic/196260-using-_post-variable-in-update-string/#findComment-1030647 Share on other sites More sharing options...
andrewgauger Posted March 23, 2010 Share Posted March 23, 2010 Try a debug of the $sql: add echo $sql after the definition and run it. I too suspect the $_POST[$products1[$i]] as producing unintended results. Quote Link to comment https://forums.phpfreaks.com/topic/196260-using-_post-variable-in-update-string/#findComment-1030649 Share on other sites More sharing options...
denoteone Posted March 23, 2010 Author Share Posted March 23, 2010 Here is more info regarding the problem. Array: $products1 = array("mocha_small", "mocha_medium", "mocha_large", "icemocha_small", "icemocha_medium", "icemocha_large", "lat_cap_small", "lat_cap_medium", "lat_cap_large", "icelat_small", "icelat_medium", "icelat_large", "coffee_small", "coffee_medium", "coffee_large", "icecoffee_small", "icecoffee_medium", "icecoffee_large", "hotchoc_small", "hotchoc_medium", "hotchoc_large"); form info: <form action="<?=$_SERVER['PHP_SELF']?>" method="POST"> <input type="hidden" name="storeid" value="<? echo $store_info; ?>"> <input type="hidden" name="storetype" value="<? echo $type; ?>"> <? for($i=0;$i<21;$i++){ if (($i + 1) % 3 == 0 ) { echo '<div style="float:left;margin-right:20px;"><input type="text" name="'.$products1[$i].'" value="'. $content[$i]['price'].' " size="6"> </div><br/><br/><br/>'; }else{ echo '<div style="float:left;margin-right:20px;"><input type="text" name="'.$products1[$i].'" value="'. $content[$i]['price'].'" size="6"> </div>'; } } ?> <div id="submitbutton"><input type="submit" value="Submit" border="0" name="submit3"></div> </form> the form loops though and shows the value as is from the database and what I am doing is letting the user make changes to the fields and then click submit to update. Quote Link to comment https://forums.phpfreaks.com/topic/196260-using-_post-variable-in-update-string/#findComment-1030653 Share on other sites More sharing options...
denoteone Posted March 23, 2010 Author Share Posted March 23, 2010 I echoed my $sql variable and I am not telling which row to update. Thanks for your guys help. I will post if I am still having issues. UPDATE price2 SET price='5.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='6.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.49' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.59' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.59' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.79' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.69' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.79' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.89' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.79' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.89' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.89' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='2.09' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='1.99' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='2.09' WHERE storeID='201' AND storeType = '1'UPDATE price2 SET price='2.19' WHERE storeID='201' AND storeType = '1' Quote Link to comment https://forums.phpfreaks.com/topic/196260-using-_post-variable-in-update-string/#findComment-1030656 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.