SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Loop in MySQL Table

    Hi Board,

    I am trying to use a PHP loop to update an MySQL table with updated data.

    When the page is loaded, an HTML table with textbox form elements is populated via a query to the MySQL database. What I would like to do is have a script that loops through each row of the table and updates the corresponding record in the database. Rather than trying to find which values in each textbox have changed, I would rather just basically update the MySQL table to reflect the HTML table/form and any new values.

    I have been struggling to get this put together, so rather than posting my sloppy code here, I thought I would ask the best way to go about it.

    Thanks in advance,
    jrculber

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <input type="hidden" name="id[0]" value="row 1's identifier" />
    <input type="text" name="textbox[0]" value="row 1's current value" />
    
    <input type="hidden" name="id[1]" value="row 2's identifier" />
    <input type="text" name="textbox[1]" value="row 3's current value" />
    
    <input type="hidden" name="id[2]" value="row 3's identifier" />
    <input type="text" name="textbox[2]" value="row 3's current value" />
    PHP Code:
    foreach ($_POST['id'] as $key => $val) {
      
    $id $val;
      
    $text $_POST['textbox'][$key];
      
    mysql_query("UPDATE sometable SET somecol = '$text' WHERE id = $id");



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •