Quick mySQL update script needed (rep available)

Discussion in 'MySQL' started by emitind, Jan 22, 2007.

  1. #1
    I have a mysql database table with two fields: "key" and "value". I have created a form which lists every key with a text field with the value.

    I want to update the values for each key when the form is submitted.

    So I'm guessing I need some kind of loop to go through each key and update the value of each.

    Havn't done this before so would appriciate a bit of script to get the Update done all by one form (instead of updating each key separately).

    Thanks for any help with this :)
     
    emitind, Jan 22, 2007 IP
  2. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is this a web based form or an application form? What are you using to code the form (PHP, ASP, etc...)? Will the form only allow them to change the value of the keys or will they be able to add a new key as well?
     
    druidelder, Jan 22, 2007 IP
  3. emitind

    emitind Peon

    Messages:
    567
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, forgot to add - web based in PHP. The form only changes (updates) the values of the keys.
     
    emitind, Jan 22, 2007 IP
  4. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Look up 'foreach' on php.net. That should point you in the right direction.
     
    rgchris, Jan 22, 2007 IP
  5. emitind

    emitind Peon

    Messages:
    567
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How do I get each key into an array?
     
    emitind, Jan 23, 2007 IP
  6. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?php
    
      # I suppose we already have a mysqli connection called $mysqli
    
      foreach ($_POST as $key => $value) {
        $pairs[] = $key . " = '" . $mysqli->real_escape_string($value) . "' ";
      }
      $update_string = implode(', ', $pairs);
      $query = 'UPDATE table SET ' . $update . ' WHERE ...'; # you should add your WHERE clause here
      $mysqli->query($query);
    
    ?>
    PHP:
    Did I earn some green? ;)
     
    picouli, Jan 24, 2007 IP
    emitind likes this.