• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

UPDATE MySQL using the URL variable

I need some help with a PHP script i'm working on.

Basically i need to create several SQL UPDATE statements based on the information in the URL.  Unfortunatley the row to update is part of the variable name, so i need to split this information out somehow.

All the variables i want to use will have their row number followed by an underscore "_" followed by their field name;

e.g.
123_gift_description = UPDATE row 123, field gift_description
27_gift_description = UPDATE row 27, field gift_description
19_name = UPDATE row 19, field name


An example URL would be;
www.MySite.com/form.php?123_gift_description=new+car&27_gift_description=space+hopper&19_name=Craig+Short

And the MySQL Updates would be;
UPDATE gift_list SET gift_description='new+car' WHERE id ='123';
UPDATE gift_list SET gift_description='space+hopper' WHERE id ='27';
UPDATE gift_list SET name='Craig+Short' WHERE id ='19';



-hopefully that made sense.....


If it help i'm only updating two fields, name and gift_description


Many thanks in Adv.
0
detox1978
Asked:
detox1978
  • 3
  • 2
2 Solutions
 
under_dogCommented:
something like this:

<?php

foreach ($_GET as $varName => $value) {
 
  $varNameArray = explode("_", $varName);
  $idnumber = $varNameArray[0];
  $fieldname = "";
  for ($i = 1; $i < count($varNameArray); $i++) {

    $fieldName .= $varNameArray[$i];

  }

  $sql = "UPDATE gift_list SET $fieldname='$value' WHERE id='$idnumber'";

  mysql_query($sql);

}

?>
0
 
detox1978Author Commented:
Very close (and very quick!), its not adding the field name to the SQL statement.  Also, it grabs all the variables, even ones without a _ in their name.

Any thoughts... :-)


+many thanks for the speedy reply..

0
 
under_dogCommented:
Case sensitivity ftw! ... and that other thing fixed:

<?php

foreach ($_GET as $varName => $value) {
 
  if (strstr($varName, "_") {

    $varNameArray = explode("_", $varName);
    $idnumber = $varNameArray[0];
    $fieldname = "";
    for ($i = 1; $i < count($varNameArray); $i++) {

      $fieldName .= $varNameArray[$i];

    }

    $sql = "UPDATE gift_list SET $fieldName='$value' WHERE id='$idnumber'";

    mysql_query($sql);

  }

}

?>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
detox1978Author Commented:
almost perfect.  It just cuts off the gift_ section of gift_description.... any idea how to work around this?
0
 
steelseth12Commented:
foreach($_GET as $key=>$value) {

      preg_match('/(_gift_description)|(_name)/',$key,$match);
      
      $temp = explode("_",$key);
      
      if($match[0]=='_gift_description'){
      
            mysql_query("UPDATE gift_list SET gift_description='".$value."' WHERE id ='".$temp[0]."'");
            
      
      }elseif($match[0]=='_name') {
      
      
            mysql_query("UPDATE gift_list SET name='".$value."' WHERE id ='".$temp[0]."'");
      
      
      
      }


}
0
 
detox1978Author Commented:
Thanks steelseth12,

worked a treat.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now