matthines Posted March 7, 2007 Share Posted March 7, 2007 I have a problem with a script. Here is a link to the working script: http://66.194.88.152/$sitepreview/autowarrantyus.com/admin.php Basically, the script prints out many different text boxes which are to be filled with insurance rates. My input names look like this: 0015*6060*9*bronze Each * seperates a different value that I extract when the page is updated. My problem may be SQL related. Basically, everything works but when I update the top value, it updates every record in the category. Here is the snippet of code I use to update the DB: foreach ($_POST as $k => $v) { print_r($_POST); $price = $v; $arr = explode("*",$k); $term = $arr[0]; $milmon = $arr[1]; $months = $milmon{0} . $milmon{1}; $miles = substr($milmon, 2); $class = $arr[2]; $level = $arr[3]; echo $term . " " . $months . " " . $miles . " " . $class . " " . $level . " " . $price; $query = mysql_query("SELECT * FROM rates WHERE class='$class' AND newterm='$term' AND level='$level' AND months='$months' AND mileage='$miles'"); $res = mysql_fetch_array($query); $id = $res["id"]; mysql_query("UPDATE rates SET price='$price' WHERE id='$id'"); $price = ""; } The problem is with the variable $class. It is a value that ranges from 1 to 14 and for some reason, when I update one value, it updates each value in the category for all classes (1-14). I need to update them individually. I have a printout when it updates so that you can see each variable and its values split up. If anyone could help, it would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/ Share on other sites More sharing options...
Ninjakreborn Posted March 7, 2007 Share Posted March 7, 2007 Well for print R, all you need to do is print_r($_POST); it will do it for you (no foreach, no while's, it does it naturally) Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-201954 Share on other sites More sharing options...
matthines Posted March 7, 2007 Author Share Posted March 7, 2007 Yea I figured that out. The problem is with assigning the value to the correct mysql row. Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-201996 Share on other sites More sharing options...
matthines Posted March 7, 2007 Author Share Posted March 7, 2007 I seperated the values to make sure I did it correctly. $class contains the proper value but when I run this command: $query = mysql_query("SELECT * FROM rates WHERE class='$class' AND newterm='$term' AND level='$level' AND months='$months' AND mileage='$miles'"); It seems to ignore only class. It updates the correct level, mileage and term ONLY but it updates every class within that scope. Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-202013 Share on other sites More sharing options...
Ninjakreborn Posted March 7, 2007 Share Posted March 7, 2007 That's why. at the end of the foreach loop (inside it but right before the closing bracket), unset each one of the variables, so they are clean when the loop starts over again. That's should fix it. Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-202030 Share on other sites More sharing options...
Daniel0 Posted March 7, 2007 Share Posted March 7, 2007 Try this: foreach($_POST as $k => $v) { $price = $v; list($term, $milmon, $class, $level) = explode("*", $k); $months = $milmon{0}.$milmon{1}; $miles = substr($milmon, 2); echo "{$term} {$months} {$miles} {$class} {$level} {$price}"; $query = mysql_query("SELECT * FROM rates WHERE class='{$class}' AND newterm='{$term}' AND level='{$level}' AND months='{$months}' AND mileage='{$miles}'"); $res = mysql_fetch_array($query); mysql_query("UPDATE rates SET price='$price' WHERE id='{$res["id"]}'"); } Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-202057 Share on other sites More sharing options...
matthines Posted March 8, 2007 Author Share Posted March 8, 2007 Thanks for the help guys! I tried the newcode and added a part to unset all of the variables. It still does not work. It acts in the exact same way. I just cant understand why it is changing 14 values. New Code: $price = $v; list($term, $milmon, $class, $level) = explode("*", $k); $months = $milmon{0}.$milmon{1}; $miles = substr($milmon, 2); echo "{$term} {$months} {$miles} {$class} {$level} {$price}"; $query = mysql_query("SELECT * FROM rates WHERE class='{$class}' AND newterm='{$term}' AND level='{$level}' AND months='{$months}' AND mileage='{$miles}'"); $res = mysql_fetch_array($query); mysql_query("UPDATE rates SET price='$price' WHERE id='{$res["id"]}'"); unset($class); unset($miles); unset($months); unset($term); unset($level); unset($price); Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-202774 Share on other sites More sharing options...
matthines Posted March 8, 2007 Author Share Posted March 8, 2007 If noone can help salvage this bit of code maybe you guys know of another way that I can create some code to go through all of the post variables and store them in a database. Essentially, this program lets someone changes price rates. There are hundreds of textboxes and I cant individually change them because that would take too much work. Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-202872 Share on other sites More sharing options...
matthines Posted March 16, 2007 Author Share Posted March 16, 2007 Apparently is was something with mySQL. I moved it to a new server and it works pefectly. thanks for everything guys! Quote Link to comment https://forums.phpfreaks.com/topic/41676-looping-through-post-vars/#findComment-208917 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.