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 6184849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:36:33+00:00 2026-05-24T01:36:33+00:00

I have a mysql database with two tables: table1 (id, name, emailid) table2 (id,

  • 0

I have a mysql database with two tables:

table1 (id, name, emailid)
table2 (id, email)
  • emailid is a relationship to table2.id

I’m trying to make an html form that lists the contents of table1 with a dropdown box for the user to select the email field. The email field is populated with <options> from table2.

My question is: how do I check what value is already selected for emailid and make that the already selected item in the dropbox when the form loads with selected="selected"?

I’ve gotten one solution to work already but I don’t think it is the proper way to do it and was looking for the best practices method.

The way I am currently doing it will only work with one item pulled from table1 but not if there are more than 1.

Here is my code so far:

This is where I get the data to make the form:

<?php
$sql = "SELECT table1.id as table1id, table1.name, table2.id as table2id
        FROM table1
        INNER JOIN table2 ON table1.emailid = table2.id
        WHERE table1.id = {$id}"; 
$result = mysqli_query($link, $sql); 
$row = mysqli_fetch_array($result); 
$items1 = array('table1id' => $row['table1id'] 
                'name' => $row['table1.name'] 
                'table2id' => $row['table2id']); 

$sql = "SELECT id, email FROM table2"; 
$result = mysqli_query($link, $sql); 
$row = mysqli_fetch_array($result); 
while ($row = mysqli_fetch_array($result)) { 
    $items2[] = array('id' => $row['id'], 
                 'email' => $row['email'], 
                 'selected' => ($row['id'] == $items1['table2id']) ? ' selected="selected" ' : ''); 
}
?>

This is my HTML:

<tr> 
    <td><?php echo $items1['name']; ?></td>
    <td><input type="hidden" name="table1id" value="<?php echo $items1['table1id']; ?>" />
        <?php echo $items1['table1id']; ?></td>
    <td>
        <select name="email"> 
        <?php foreach ($items2 as $item2): ?> 
            <option value="<?php echo $item2['id']; ?>"<?php echo $item2['selected']; ?>>
            <?php echo $item2['email']; ?></option> 
        <?php endforeach; ?>
        </select>
    </td> 
</tr>

This is how the form is updated:

<?php
$id = mysql_real_escape_string($_POST['table1id']); 
$email = mysql_real_escape_string($_POST['email']); 

$sql = "UPDATE table1 SET emailid = {$email} WHERE id = {$id}"; 
?>
  • 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-05-24T01:36:34+00:00Added an answer on May 24, 2026 at 1:36 am

    EDIT
    Killed that other stuff now that you have code up.

    // Lets make our table2 the primary so we can organize our values in the $items array.
    $sql = "SELECT table1.id as table1id, table1.name, table2.id as table2id, table2.email 
        FROM table2
        LEFT JOIN table1 ON table2.emailid = table1.id";
        // Do you need this WHERE?  I'm thinking not.
        // WHERE table1.id = {$id}";  
    $items = array();
    while($row = mysqli_fetch_array($result)) {
      // This will get set every time through the loop, but this is ok.
      // You can check to see if its set and then set or not set.
      $items[$row['table1id']]['name'] = $row['name'];
      // We will keep appending email values here to the table1id array.
      $items[$row['table1id']]['emails'][] = $row['email'];
    }
    

    Lets do the HTML markup now that we have all the items in a nice array for us.

    <?php foreach($items as $id => $item): ?>    
    <tr> 
      <td><?php echo $item['name']; ?></td>
      // Do you really need this hidden field?
      <td><input type="hidden" name="table1id" value="<?php echo $id; ?>" /><?php echo $id; ?></td>
      <td>
        // Updated this to be unique for each id iteration.
        <select name="email_<?php echo $id; ?>">
        // Might want to consider a is_array() check on this value.
        <?php foreach ($item['email'] as $email): ?>
            <?php $selected = $email == $_POST['email_'. $id] ? ' selected="selected"' : ''; ?>
            <option value="<?php echo $email; ?>"<?php echo $selected; ?>>
            <?php echo $email; ?></option> 
        <?php endforeach; ?>
        </select>
      </td> 
    </tr>
    <?php endforeach; ?>
    

    I have not tested this so it may not work as a copy and paste. But I think this is can get you going correctly now.

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

Sidebar

Related Questions

I have a MySQL database with two tables in it: Members - id, name,
I have two tables in a Mysql database. PACKAGES: ID (auto_increment) CATEGORY (INT) NAME
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables in a MySQL database, Locations and Tags, and a third
If I have two tables in a MySQL database that both have a column
I have two MySQL database tables: one containing a list of championships and another
I am trying to import data from a large database. I have two tables
I have a MySQL database with a lot of tables, and I have two
I have two tables in a database that looks like this members table1 id
I have two tables in my SQL database: mysql> select *from crop; +------+-----------+----------+ |

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.