SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to update multiple records using arrays...

    Okay, here goes...

    I set up a page called edit.php which allows an admin to edit news articles. The admin sees the article list, checks the articles he'd like to update, submits the form which then brings up the articles that he selected for editing. Once they're edited, the admin submits that form and the records are updated.

    I got through the first few steps with little resistance. I've gotten to where the admin can check the articles he'd like to update, and they show up in a form for editing:

    Here's the form (which works correctly):
    PHP Code:
    <?
    if(($_GET['type']) == 'news' && ($_GET['show']) == && empty($_GET['status'])) {
        
        if(
    is_array($_POST['article_id'])) {
        
            foreach (
    $_POST['article_id'] as $edit_article_id) {
        
                
    $retrieve_articles mysql_query("
                SELECT *
                FROM article
                WHERE article_id='
    $edit_article_id'");
                if(!
    $retrieve_articles) {
                echo 
    mysql_error;
                exit;
                }
                
    ?>
        
                <form name="edit_article" action="edit.php?type=news&status=edit" method="post">

                <?    
                
    while($row mysql_fetch_array($retrieve_articles)) {
                
    $article_id $row['article_id'];
                
    $article_title $row['article_title'];
                
    $article_body $row['article_body'];
                
    ?>
        
                <div>    
                    <label>Title: </label>
                </div>    
                <input name="edit_article_title[]" value="<? echo $article_title?>" type="text"> &nbsp;<strong>id</strong>: <? echo $article_id?>
                <div>    
                    <label>Body: </label>
                </div>
                <textarea name="edit_article_body[]" rows="9" cols="65"><? echo $article_body?></textarea>
                <div>
                <input name="edit_article_id[]" value="<? echo $article_id?>" type="hidden">
                </div>
                <?    
                
    }
                
            }
            
    ?>
            
                        <input name="edit_article_submit" value="Submit" type="submit">
                </form>
            
            <?

        
    }

    }
    ?>
    The next step is for the admin to edit the articles, click submit and the articles should update. Here's where I get stuck.

    This is the code that should update the records:
    PHP Code:
    <?
    // If the edit news form is submitted, update the news
    if(($_GET['type']) == 'news' && isset($_POST['edit_article_submit']) && is_array($_POST['edit_article_id']) && ($_GET['status']) == 'edit') {

        
    #####################################################################
        ## I realize it probably has something to do with these two pieces
        #####################################################################
        
    $edit_article_title $_POST['edit_article_title'];
        
    $edit_article_body $_POST['edit_article_body'];
        
    #####################################################################
        
        
    foreach ($_POST['edit_article_id'] as $edit_article_id) {
        
            
    $update_articles mysql_query("
            UPDATE article
            SET article_id='
    $edit_article_id', article_title='$edit_article_title', article_body='$edit_article_body'
            WHERE article_id='
    $edit_article_id'
            "
    );
            if(!
    $update_articles) {
            echo 
    mysql_error();
            exit;
            }
                
        }
            
        
    header("Location: edit.php?type=news&status=done");
        
    }
    ?>
    When I submit the form with the code above, the article_id value gets inserted correctly, but article_title and article_body both show up in my DB as "Array."

    I've made a note above the section of code that I think is giving me the problem. Any ideas?

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you name an HTML input in the form of name[] then when it gets pulled into PHP as a POST variable, it's an array. So the first edit_article_body would be $_POST['edit_article_body'][0] and the second would be $_POST['edit_article_body'][1].

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the same problem once. What happens is that the post contains an array of all articles ids, an array of titles and an array of bodies. Each article part shares the same key in each one of those three arrays.
    You should then use something like:
    PHP Code:
    foreach ($_POST['edit_article_id'] as $key => $edit_article_id) { 
         
            
    $update_articles mysql_query(
            UPDATE article 
            SET article_id='
    $edit_article_id[$key]', article_title='$edit_article_title'[$key], article_body='$edit_article_body[$key]
            WHERE article_id='
    $edit_article_id[$key]
            "
    ); 
            if(!
    $update_articles) { 
            echo 
    mysql_error(); 
            exit; 
            } 
                 
        } 
    Another method, which is the one I prefer, since it keeps things clearer, is to "reverse" the array before using the data (for validation, SQL inserts/updates,...). The resulting array will contain one array for each article (the id, the title and the body).

    The explanation is probably a bit confusing, so check this code:
    PHP Code:
    <?php
    $post 
    = array("id" => array("#1""#2""#3"),
                    
    "title" => array("title 1""title 2""title 3"),
                    
    "body" => array("body 1""body 2""body 3")
                    );
    echo 
    "<pre>post "print_r($post); echo "</pre>";

    function 
    reverse_array($array) {
        
    $reversed = array();
        foreach(
    $array as $array_key => $array_values) {
            foreach(
    $array_values as $key => $value) {
                
    $reversed[$key][$array_key] = $value;
            }
        }
        return 
    $reversed;
    }

    $reversed_post reverse_array($post);
    echo 
    "<pre>reversed_post "print_r($reversed_post); echo "</pre>";
    ?>
    ...and the resulting output:
    HTML Code:
    post Array
    (
        [id] => Array
            (
                [0] => #1
                [1] => #2
                [2] => #3
            )
    
        [title] => Array
            (
                [0] => title 1
                [1] => title 2
                [2] => title 3
            )
    
        [body] => Array
            (
                [0] => body 1
                [1] => body 2
                [2] => body 3
            )
    
    )
    reversed_post Array
    (
        [0] => Array
            (
                [id] => #1
                [title] => title 1
                [body] => body 1
            )
    
        [1] => Array
            (
                [id] => #2
                [title] => title 2
                [body] => body 2
            )
    
        [2] => Array
            (
                [id] => #3
                [title] => title 3
                [body] => body 3
            )
    
    )
    Hope that helps.

    MichaŽl
    MichaŽl Niessen
    http://assemblysys.com
    (Countries/states/cities with latitude & longitude,
    weathercodes & topical databases)

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mniessen
    I had the same problem once. What happens is that the post contains an array of all articles ids, an array of titles and an array of bodies. Each article part shares the same key in each one of those three arrays.
    You should then use something like:
    PHP Code:
    foreach ($_POST['edit_article_id'] as $key => $edit_article_id) { 
         
            
    $update_articles mysql_query(
            UPDATE article 
            SET article_id='
    $edit_article_id[$key]', article_title='$edit_article_title'[$key], article_body='$edit_article_body[$key]
            WHERE article_id='
    $edit_article_id[$key]
            "
    ); 
            if(!
    $update_articles) { 
            echo 
    mysql_error(); 
            exit; 
            } 
                 
        } 
    That doesn't update either of the values...any idea why not?

    PS. I think I'll stick with this rather than the second part you posted, at least until I get more comfortable with arrays.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops... I forgot to replace "$edit_article_id" from the SQL...

    PHP Code:
    foreach ($_POST['edit_article_id'] as $key => $edit_article_id) { 
          
            
    $update_articles mysql_query(
            UPDATE article 
            SET article_id='
    $_POST[edit_article_id][$key]', article_title='$_POST[edit_article_title][$key], article_body='$_POST[edit_article_body][$key]' 
            WHERE article_id='
    $_POST[edit_article_id][$key]' 
            "
    ); 
            if(!
    $update_articles) { 
            echo 
    mysql_error(); 
            exit; 
            } 
                  
        } 
    should work.

    Also, it's not necessary to update the "article_id" field, because it doesn't change...

    MichaŽl
    MichaŽl Niessen
    http://assemblysys.com
    (Countries/states/cities with latitude & longitude,
    weathercodes & topical databases)

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mniessen
    Oops... I forgot to replace "$edit_article_id" from the SQL...

    PHP Code:
    foreach ($_POST['edit_article_id'] as $key => $edit_article_id) { 
          
            
    $update_articles mysql_query(
            UPDATE article 
            SET article_id='
    $_POST[edit_article_id][$key]', article_title='$_POST[edit_article_title][$key], article_body='$_POST[edit_article_body][$key]' 
            WHERE article_id='
    $_POST[edit_article_id][$key]' 
            "
    ); 
            if(!
    $update_articles) { 
            echo 
    mysql_error(); 
            exit; 
            } 
                  
        } 
    should work.

    Also, it's not necessary to update the "article_id" field, because it doesn't change...

    MichaŽl
    This doesn't insert anything either :
    PHP Code:
        foreach ($_POST['edit_article_id'] as $key => $edit_article_id) {
          
            
    $update_articles mysql_query("
            UPDATE article
            SET article_title='
    $_POST[edit_article_title][$key]', article_body='$_POST[edit_article_body][$key]'
            WHERE article_id='
    $_POST[edit_article_id][$key]'
            "
    );
            if(!
    $update_articles) {
            echo 
    mysql_error();
            exit;
            }
                  
        } 

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    foreach ($_POST['edit_article_id'] as $key => $edit_article_id) { 
           
            
    $update_articles mysql_query("UPDATE article 
            SET article_id='" 
    $_POST['edit_article_id'][$key] . "', article_title='" $_POST['edit_article_title'][$key] . "', article_body='" $_POST['edit_article_body'][$key] . "' 
            WHERE article_id='" 
    $_POST['edit_article_id'][$key] . "'"); 
            if(!
    $update_articles) { 
            echo 
    mysql_error(); 
            exit; 
            } 
                   
        } 
    If that doesn't work, I don't understand what is the problem...

    MichaŽl
    MichaŽl Niessen
    http://assemblysys.com
    (Countries/states/cities with latitude & longitude,
    weathercodes & topical databases)

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mniessen
    Try this:
    PHP Code:
    foreach ($_POST['edit_article_id'] as $key => $edit_article_id) { 
           
            
    $update_articles mysql_query("UPDATE article 
            SET article_id='" 
    $_POST['edit_article_id'][$key] . "', article_title='" $_POST['edit_article_title'][$key] . "', article_body='" $_POST['edit_article_body'][$key] . "' 
            WHERE article_id='" 
    $_POST['edit_article_id'][$key] . "'"); 
            if(!
    $update_articles) { 
            echo 
    mysql_error(); 
            exit; 
            } 
                   
        } 
    If that doesn't work, I don't understand what is the problem...

    MichaŽl
    That worked perfectly! Thanks for all the help. I'll keep in mind that they need to be broken out like that to work.

  9. #9
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad I could help!

    Also, don't forget about my "reversed array" method. I've found it's a lot easier to work with the data when everything is kept together in a single array...

    MichaŽl
    MichaŽl Niessen
    http://assemblysys.com
    (Countries/states/cities with latitude & longitude,
    weathercodes & topical databases)


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
  •