multiple input boxes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a form that I'm creating that queries my database for active users,  
puts them in a list and then adds an input box for each line.  I'm having  
troubles trying to figure out how I would then input the information from  
the input box into my table when submitted.  Each line lists out the id,  
first name, last name from the query, but each input box is named the same.  
How would I change the name of each input box?

Hopefully this makes some sense.  Thanks for any help that you can provide.


Re: multiple input boxes

don't name them the same!  :)

if you're going through a loop

$name = $i.'_id';
$name = $i_'first_name';

or, a bit more clever $name = 'user['.$i.'][id];
ie <INPUT name="user[1][id]">
<INPUT name="user[1][first_name]">
<INPUT name="user[1][last_name]">
<INPUT name="user[2][id]">

will create a $_POST var that looks like this when submitted

$_POST = array(
     user = array(
          1 => array(
               'id' => 'whatever',
               'first_name' => 'whatever',
               'last_name' => 'whatever',
          2 => array(
               'id' => 'whatever',

Auddog wrote:
Quoted text here. Click to load it

Re: multiple input boxes

please pardon my typos in my post... hopefully you get the idea.

Re: multiple input boxes

Quoted text here. Click to load it

Here is where I'm currently at in my programming:

<div align="center">
  <table width="300" border="1" cellpadding="4" style='border-collapse:  
      <td width="50" bgcolor="#CCCCCC"><div  
      <td width="125" bgcolor="#CCCCCC"><strong>First Name</strong></td>
      <td width="125" bgcolor="#CCCCCC"><strong>Last Name</strong></td>
      <td width="100" bgcolor="#CCCCCC"><strong>Hours</strong></td>


include 'config.php';

/*** create a new mysqli object with default database***/
$connection = mysqli_connect($hostname, $username, $password, $dbname) or  
die ("Unable to connect");

//create query
$query = "SELECT id, fname, lname FROM prod_employee where active = 'yes'";

//excute query
$result = mysqli_query($connection, $query) or die ("Error in query: $query.  

// see if any rows were returned
if (mysqli_num_rows($result) > 0) {
    // yes
    // print them one after another
        while (list($id, $fname, $lname) = mysqli_fetch_row($result))
            echo "    <tr>
      <td><div align=center>$id</div></td>
      <td><input name=hours type=text size=4 maxlength=4 />
    echo "</table>";
else {
    // no
    // print status message
    echo "No rows found!";

// free result set memory

// close connection


The input box would be the hours input - I'm not sure I catch on to what you  
would do.  Thanks for the help.


Re: multiple input boxes

Auddog wrote:
Quoted text here. Click to load it

perhaps instead of '<input name=hours type=text size=4 maxlength=4 />'
you do ''<input name="'.$id.'_hours" type="text" size=4 maxlength=4

I assume that 'id' is unique for each from

Re: multiple input boxes

Quoted text here. Click to load it

This will make each input name unique by adding the '.$id.'_  - correct?  
which is also the same id for each user?  Then I can use the $_POST = array  
to input into my database.  

Site Timeline