www.webdeveloper.com
Results 1 to 4 of 4

Thread: Hi, i want to update several rows in my table with different information

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Posts
    9

    Question Hi, i want to update several rows in my table with different information

    Hi, i have a code like this:
    PHP Code:
              <?php
              $o    
    mysql_query("SELECT * FROM `$mysql_data`.`category_Table` WHERE name!='Tomt felt' AND username='$userName'");
              while(
    $out mysql_fetch_object($o)){
              
    ?>  
                <tr>
                  <td width="11">&nbsp;</td>
                  <td width="153"><input type="text" name="<?php echo "what should i have here? :s";?>" value="<?php echo "$out->name"?>"></td>
                  <td width="93">Gå til kategori</td>
                  <td width="25" align="center">x</td>
                  <td width="24" align="center">e</td>
                </tr>
               <?php
              
    }
               
    ?>
    what can i do, to make it update etc 10 categories in the same form, with the same name?

    think foreach and array is the thing, but i dont remeber that part at all anymore :s.

    anyone with a solution?

    norNerd

  2. #2
    Join Date
    Jul 2005
    Location
    USA
    Posts
    909
    name="category[<?php echo $out->id;?>]"
    Bitter web veteran

  3. #3
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Your existing code already correctly loops through the result set.

    As to the second part, "what can i do, to make it update etc 10 categories in the same form, with the same name?" makes absolutely no sense to me plus the select * doesn't tell us what fields you're pulling anyway if you wanted to use one of those. I'm thinking too it's one of the fields in that table, maybe a category ID as was mentioned by the other respondent. On a side note, you should only select the fields you're using unless you're using them all. FYI - to optimize you queries.

    You mentioned to think "array" in your comments so if you're asking how a form name attribute can contain mulitiple values, the solution is to simply append [] on the end, i.e. "name='foobar[]'". But that's not used in an INPUT tag, so just use the $o->whatever_name_it_is as was suggested.


    -jim
    Last edited by SrWebDeveloper; 12-04-2009 at 12:04 PM.

  4. #4
    Join Date
    Nov 2009
    Posts
    9

    Smile Got it :)

    Hi, sorry for not explaining so good, but found a solution:

    Output the categories
    PHP Code:
              <?php
                  $pat_cat 
    mysql_query("SELECT * FROM `$mysql_data`.`category_Table` WHERE username='$userName' ORDER BY id DESC");
                  while(
    $out mysql_fetch_assoc($pat_cat)){
              
    ?>  
                <tr>
                  <td width="12">&nbsp;</td>
                  <td width="132"><span style="color:#444444; font-size:13px; font-family:'Courier New', Courier, monospace; cursor:pointer;"> G&aring; til kategori </span></td>
                  <td width="170"><input type="text" name="name_<?php echo $out['id'];?>" value="<?php echo $out['name']; ?>"></td>
                </tr>
               <?php
              
    }
               
    ?>
    Then update the categories:
    PHP Code:
    <?php
    if(isset($_POST['save_categories'])){

        
    $update mysql_query("SELECT * FROM `$mysql_data`.`category_Table` WHERE username='$userName'");
        while(
    $up_row mysql_fetch_assoc($update)){
            
    $name $_POST['name_'.$up_row[id]];
                
                
    mysql_query("UPDATE `$mysql_data`.`category_Table` SET name='$name' WHERE id='$up_row[id]'");
        }
                
            echo 
    "<script>alert('Categories updated.');</script>";
            echo 
    "<script>window.close();</script>";
    }
    ?>
    Cheers!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles