Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Link drop down menu to MySQL

Featured Replies

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 by Willows

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.

  • 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 by Willows

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 by Renaissance-Design

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.