Solved

UPDATE MySQL using the URL variable

Posted on 2007-03-25
6
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

729 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