Jump to content
itgranny

Display vs POST info in a mysql based dropdown

Recommended Posts

I have a mysql table where the columns are id, name, username and password.

 

I have it working so that names are in the dropdown, its its posting it, but is there a way to make it display the name, yet post the id?  The following is what I have done, it works, but the id is just a number and means nothing to the user. When I trade that out for name, the user is able to read and understand it, but some of the names have spaces, punctuation ect, and I would like to have it to be able to look up the username and password once the main table is to be updated. 

$sql = "SELECT * FROM stud ORDER BY studName";
$result = mysql_query($sql);
echo "<td>  <select name='id'>";
while ($row = mysql_fetch_array($result)) {
echo "<option style='text-transform: uppercase;'" . $row['id'] . "'id='dropdown' >" . $row['id'] . "</option>";
}

echo "</select></td></tr>";
echo "<tr><td><input id='submit' name='submit' type='submit' value='Submit'></td></tr></table> </form>";

basically what I want is this but I can't get this to work. 

$sql = "SELECT * FROM stud ORDER BY studName";
$result = mysql_query($sql);
echo "<td>  <select name='id'>";
while ($row = mysql_fetch_array($result)) {
echo "<option style='text-transform: uppercase;'" . $row['id'] . "'id='dropdown' >" . $row['name'] . "</option>";
}

echo "</select></td></tr>";
echo "<tr><td><input id='submit' name='submit' type='submit' value='Submit'></td></tr></table> </form>";

Share this post


Link to post
Share on other sites

if you have

<option value='$id'>$name</option>

then the name is displayed but the id (value) is posted.

Share this post


Link to post
Share on other sites

The problem is your missing value=" " (I think that's what @Barand was pointing out.)

 

You can also clean up your php by letting Mysql do the uppercase

 

SELECT id,UPPER(name) FROM stud ORDER BY studName 

 

​and by using parenthesis instead of escaping your variables.

echo "<option value=\"{$row['id']}\" id=\"dropdown\" >{$row['name']}</option>";
Edited by benanamen

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.