Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:42:53+00:00 2026-06-09T23:42:53+00:00

I have a PHP form that pulls data from the database, prints it out

  • 0

I have a PHP form that pulls data from the database, prints it out in rows, and then allows the user to check which items they need. On save, only the very last option is being sent through to $_POST. Here’s my code:

global $dbc; 

$afQuery = "SELECT fid, facility_name, city, state, country, phone from facilities 
            WHERE uid = '123'
            AND fid != '456'
            GROUP BY fid;";
        $afResult = $dbc->query($afQuery,__FILE__,__LINE__);
        $i = 0;
        while ($affiliated = $dbc->fetch($afResult)) {      
            ++$i;
            echo '<div class="action" style="float:left;">';
                echo '<input type="checkbox" name="affiliated[afid]" id="afid" value="'.$affiliated->fid.'">';
            echo '</div>';
            echo '<div class="facility-name">';
                echo '<input type="hidden" id="facility-name '.$i.'" name="affiliated[facility_name]" value="'.$affiliated->facility_name.'" />';
                echo $affiliated->facility_name;
            echo '</div>';
            echo '<div class="facility-location">';
                echo '<input type="hidden" id="facility-location '.$i.'" name="affiliated[facility_location]" value="'.$affiliates->city. ', ' . $affiliated->state . ' ' . $affiliated->country.'" />';
                echo $affiliated->city. ', ' . $affiliated->state . ' ' . $affiliated->country;
            echo '</div>';
            echo '<div class="facility-phone">';
                echo '<input type="hidden" id="facility-phone '.$i.'" name="affiliated[facility_phone]" value="'.$affiliated->phone.'" />';
                echo $affiliated->phone;
            echo '</div>';
            echo '<div class="clear"></div>';
            echo '<input type="hidden" id="update" name="update" value="' . $affiliated->fid . '" />';
        }

$dbc->fetch is equivalent to mysql_fetch_assoc.

I am trying to save these values to the $affiliated array, but it should have sub-arrays for each selected item, so I can store each value in the database on a separate row. Where am I missing it? I tried a foreach within the while loop, but I got nowhere with that.

Thanks for your help!

* UPDATE *

I have followed @Pinetree’s suggestion and added $i like so:

<input type="checkbox" name="affiliated[afid]['.$i.']" id="afid" value="'.$affiliated->fid.'">

However, now I receive the following output:

Array
(
    [afid] => Array
        (
            [1] => 5289
            [2] => 5290
            [3] => 5291
            [4] => 5292
        )

    [facility_name] => Array
        (
            [1] => Test Company
            [2] => Test Company 2
            [3] => Test Company 3
            [4] => Test Company 4
        )

    [facility_location] => Array
        (
            [1] => Address, State US
            [2] => Address, State US
            [3] => Address, State US
            [4] => Address, State US
        )

    [facility_phone] => Array
        (
            [1] => 555-555-5555
            [2] => 555-555-5555
            [3] => 555-555-5555
            [4] => 555-555-555
        )

)

I need this to instead look like this so I can insert each complete row into a table in the database:

Array
    (
        [0] => Array
            (
                [afid] => 5289
                [facility_name] => Test Company
                [facility_location] => Address, ST USA
                [facility_phone] => 555-555-5555
            )

    [1] => Array
        (
            [afid] => 5290
            [facility_name] => Test Company 1
            [facility_location] => Address, ST USA
            [facility_phone] => 555-555-5555
        )
)

Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:42:54+00:00Added an answer on June 9, 2026 at 11:42 pm

    If I understood correctly, you are echoing multiple rows of inputs.
    If that is the case, you are using the same name for the inputs of each row.
    When the post happens, it will only take the last value.
    You could add another “[]” to each input name:

    '<input name="affiliated[afid][]"/>'
    

    or even put the $i in it:

    '<input name="affiliated[afid]['.$i.']"/>'
    

    In the post you’d then have:

    affiliated
        afid
            1: val
            2: val
            3: val
            ...
        facility_name
            1: val
            2: val
            3: val
            ...
        ...
    

    the integers correspond to the row in the original form. So afid[1] and facility_name[1] are from the same row in the form (and table)

    EDIT:
    To get the arrays back to the format they had in the form:

    $affiliated = array();
    foreach($_POST['affiliated']['afid'] as $k => $v) {
        $affiliated[] = array(
            'afid' => $v,
            'facility_name' => $_POST['affiliated']['facility_name'][$k] // same key here
             // other fields
        );
    }
    

    IMPORTANT EDIT:

    Since you have checkboxes, be sure to explicitly set an index int the input name:

    '<input name="affiliated[afid]['.$i.']"/>'
    

    As checkboxes are not posted if not checked, and text fields and similar are, you could end up with a situation, for instance, the second POSTED checkbox (index 1 if starting from 0) will map to the second row in the $_POST, but the checkbox was actually on the third or fourth row in the form itself and the checkboxes before that one were not posted

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that pulls data from a database. Code works fine if
I have a form text field that pulls a date from a database (in
I have a PHP script that pulls keywords from a MySQL database and I
I have a php form that saves the info to my database and sends
i have a PHP contact form that submits data, and an email...: <?php $dbh=mysql_connect
I have an html/php form that updates entries on the database server. I need
I have this file 'gardens.php' , which pulls data from a table called 'generalinfo'
I have a php file which pulls some data from external API's, and I
I have a form that a user fills out, which submits information to my
I have an html table that pulls data from a DB. It has an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.