drax Posted March 18, 2008 Share Posted March 18, 2008 Hi there, I was hoping for a little help a with project please. In a form I have a drop down menu that selects records in database and displays them as options to the user. The table that is searches has only 2 fields (ID & name). On the Drop down i have it so the user sees the name of the record and not the ID of it, but at the moment it UPDATES only the ID and not the name. How do i make it so both the ID and Name are UPDATED from using just the drop down??????? If anybody could help that would be ace! Thanks Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 18, 2008 Share Posted March 18, 2008 Can you be more specific as to what you mean by update and at what point anything is updated? Maybe some code, too. Quote Link to comment Share on other sites More sharing options...
drax Posted March 18, 2008 Author Share Posted March 18, 2008 Hi, yes sorry, it is a form and in it is 1 drop down menu and a submit button (onclick the record is INSERTED). The drop down menu displays records that have been searched from a table in my database (ID and Name). in the option it displays the Name to the user but INSERTS the ID. I need it to not only INSERT the ID value from the drop down into the ID field of that table BUT to ALSO INSERT the Name. The Drop Down has both of those details as it displays the name and INSERTS the ID. Heres the code of the INSERT: if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO table (bID, aID, ID, name) VALUES (%s, %s, %s, %s)", GetSQLValueString($_POST['ID'], "int"), GetSQLValueString($_POST['aID'], "int"), GetSQLValueString($_POST['select'], "int"), I wanted to try to add the Name here in the INSERT, I tried linking it to select, text. I also tried linking to a hidden field <? echo $row_list['Name']?> to it.... Heres the code for the drop down: [code]<select name="select"> <?php do { ?> <option value="<?php echo $row_list['ID']?>"><?php echo $row_list['Name']?></option> <?php } while ($row_list = mysql_fetch_assoc($list)); $rows = mysql_num_rows($list); if($rows > 0) { mysql_data_seek($list, 0); $row_list = mysql_fetch_assoc($list); } [/code] Please help me Thanks Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 if you have the id of the name, you don't need to store the name. use the stored id to look up the name when you need to. Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 18, 2008 Share Posted March 18, 2008 If you really want it, you can query the same table that you originally made the drop down from: INSERT INTO table (ID, Name) VALUES ($_POST['ID'], (SELECT Name FROM othertable WHERE ID = $_POST['ID'])) But like BlueSkyIS said, it is redundant to store the same data like. If you wanted to do it from the form you would have to make some hidden values that are changed before submision with javascript. Something like: <input type=hidden name="name"> <input type=submit onclick="name.value=select.value;formname.submit()"> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 yes. you have to use the ID to either look up the name now or later. might as well make it later to keep your database normalized. the javascript way will work MOST of the time, but not always, and should be avoided in favor of the pure PHP option. Quote Link to comment Share on other sites More sharing options...
drax Posted March 18, 2008 Author Share Posted March 18, 2008 Hi, Thanks for all your help. But then how would i be able to select the appropriate names to display back to the user because it would need to search one table to find all the IDs of one (url parameter) and then with all IDs known that would then need to be queried against the original table that has both ID and name. How would you do that?? Thanks Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 use a statement like SELECT A.*, B.* FROM some_table A, other_table B WHERE A.user = '$user_criteria' AND A.id = B.id Quote Link to comment Share on other sites More sharing options...
drax Posted March 18, 2008 Author Share Posted March 18, 2008 Hi, thanks very much for replying, I'm a bit of a newbie at this, bit confused about the $user_critera bit where else would that need to be used???? I'm using 3 tables here Main ID, experience (ID & Name) and actual_experience (holds ID of both Main and experience). How would i implement this??? Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.