?
Solved

UPDATE MySQL using the URL variable

Posted on 2007-03-25
6
Medium Priority
?
197 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1400 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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