Jump to content

Looping through POST vars


matthines

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"]}'");
}

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.