June 16, 201214 yr I have a form which directly updates information in my database.. The database displays 2 fields, Name , Points You type a name into the form and change their points accordingly. Now rather than type the name In I figured It would be easier to set up a drop down list where you just select the name, I have managed to do this but only by inserting the names manually into the form html.. Is there a way I can link the drop down menu to display all of the names from my database in the Column 'Name' alphabetically? so they can be viewed and selected at ease? any advice will do guys, thanks! EDIT: also when new names are added to the db they will they be included in the drop down..?? FORM Tournement League Updater <form action="tourneyleaguepost.php" method="post"> <select name="Name" /> <option>--SELECT NAME--</option> <option>Willows</option> <option>Peter North</option> <option>Name Name</option> <input name="Points" type="text" value="Insert points" size="8" style="color:#666;" /><input type="submit" value="Update" /> </form> PHP <?php define('DB_NAME', 'TourneyLeague'); define('DB_USER', 'root'); define('DB_PASSWORD', 'pw'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); } $value = $_POST['Name']; $value2 = $_POST['Points']; $query="SELECT points FROM tourneytable WHERE Name='$value'"; $executequery=mysql_query($query); $array=mysql_fetch_array($executequery); if ($array[0]<$value2) { $query2="UPDATE tourneytable SET Points='$value2' WHERE Name='$value'"; mysql_query($query2); } if (!mysql_query($query)) { die('Error: ' . mysql_error()); } mysql_close(); ?> <?php header( 'Location: index.php' ) ; ?> Edited June 16, 201214 yr by Willows
June 16, 201214 yr Solved, close thread - Thanks When you solve something it's generally considered polite to post the solution so other people with the same problem can benefit from it.
June 16, 201214 yr Author Apologize, let me post it now, I already stated Im pretty new to forums so not too sure of the 'etiquette' you know, I changed my form to this; Tournement League Updater <form action="tourneyleaguepost.php" method="post"> <select id="Name" name="Name"> <?php $mysqlserver="localhost"; $mysqlusername="root"; $mysqlpassword="pw"; $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); $dbname = 'TourneyLeague'; mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error()); $namequery="SELECT Name FROM tourneytable ORDER BY `Name` ASC "; $nameresult=mysql_query($namequery) or die ("Query to get data from firsttable failed: ".mysql_error()); while ($row=mysql_fetch_array($nameresult)) { $Name=$row[Name]; echo "<option> $Name </option>"; } ?> </select> <input name="Points" type="text" value="Insert points" size="8" style="color:#666;" /><input type="submit" value="Update" /> </form> I had to create a query to select a NAME from my table and ORDER them in the drop down menu. Thanks Edited June 16, 201214 yr by Willows
June 16, 201214 yr Spot on, nicely done Only thing I'd change is to select the ID as well and pass it in the value attribute of the <option>. Always refer to things internally by unique ID and keep the names for display purposes - it avoids collisions when dealing with two people with the same name, for example. Edited June 16, 201214 yr by Renaissance-Design
Create an account or sign in to comment