Solved

UPDATE MySQL using the URL variable

Posted on 2007-03-25
6
191 Views
Last Modified: 2013-12-13
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
Comment
Question by:detox1978
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:under_dog
ID: 18788642
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
 
LVL 2

Author Comment

by:detox1978
ID: 18788672
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
 
LVL 9

Assisted Solution

by:under_dog
under_dog earned 150 total points
ID: 18788699
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Author Comment

by:detox1978
ID: 18788735
almost perfect.  It just cuts off the gift_ section of gift_description.... any idea how to work around this?
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 350 total points
ID: 18788768
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
 
LVL 2

Author Comment

by:detox1978
ID: 18789203
Thanks steelseth12,

worked a treat.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now