Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check whether dropbox item is selected?
    primarykey
    data
    text
    <p>I have a mysql database with two tables:</p> <pre><code>table1 (id, name, emailid) table2 (id, email) </code></pre> <ul> <li><code>emailid</code> is a relationship to <code>table2.id</code></li> </ul> <p>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 <code>&lt;options&gt;</code> from <code>table2</code>.</p> <p>My question is: how do I check what value is already selected for <code>emailid</code> and make that the already selected item in the dropbox when the form loads with <code>selected="selected"</code>?</p> <p>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.</p> <p>The way I am currently doing it will only work with one item pulled from <code>table1</code> but not if there are more than 1.</p> <p>Here is my code so far:</p> <p>This is where I get the data to make the form:</p> <pre><code>&lt;?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' =&gt; $row['table1id'] 'name' =&gt; $row['table1.name'] 'table2id' =&gt; $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' =&gt; $row['id'], 'email' =&gt; $row['email'], 'selected' =&gt; ($row['id'] == $items1['table2id']) ? ' selected="selected" ' : ''); } ?&gt; </code></pre> <p>This is my HTML:</p> <pre><code>&lt;tr&gt; &lt;td&gt;&lt;?php echo $items1['name']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="hidden" name="table1id" value="&lt;?php echo $items1['table1id']; ?&gt;" /&gt; &lt;?php echo $items1['table1id']; ?&gt;&lt;/td&gt; &lt;td&gt; &lt;select name="email"&gt; &lt;?php foreach ($items2 as $item2): ?&gt; &lt;option value="&lt;?php echo $item2['id']; ?&gt;"&lt;?php echo $item2['selected']; ?&gt;&gt; &lt;?php echo $item2['email']; ?&gt;&lt;/option&gt; &lt;?php endforeach; ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>This is how the form is updated:</p> <pre><code>&lt;?php $id = mysql_real_escape_string($_POST['table1id']); $email = mysql_real_escape_string($_POST['email']); $sql = "UPDATE table1 SET emailid = {$email} WHERE id = {$id}"; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload