Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

UPDATE MySQL using the URL variable

Posted on 2007-03-25
6
Medium Priority
?
200 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
Technology Partners: 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!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

636 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