How to make data in mysql only selectable once via a webform?

Problem :

I'm building a simple web form (or tying to!) which displays a list of football teams. This list of teams is located in a single column mysql table. How can I make it so that if a team has been selected on the web form, that it cannot be selected again on the form? i.e. to make sure they are not accidentally selected to play each other, or play twice at the same time. My code so far is as below, which seems to be working fine. It includes several other iterations of the same code for the other home teams and away teams. Any pointers/help greatly appreciated.

<select name="hometeam">
$query = "SELECT * FROM teams order by teamname";

$result = mysql_query($query);

while($row = mysql_fetch_array($result))
  echo "<option value=\"".$row['teamname']."\">".$row['teamname']."</option>\n  ";

<select name="hometeam2">
$query = "SELECT * FROM teams order by teamname";

$result = mysql_query($query);

while($row = mysql_fetch_array($result))
  echo "<option value=\"".$row['teamname']."\">".$row['teamname']."</option>\n  ";


Solution :

I Method: A possible and simple solution of this can be through javascript. That is, once the selection has been made at both the combo-boxes, then before submitting the form to the server, you can compare the two values and if they are found to be same then you can alert the user about this.

Also, just to add, though you validate using javascript, but its always advisable to re-validate at the server side to avoid any hack. Hope this helps.

II Method: If you don't want to show the team chosen in one drop-down in other drop-down then for this you will have to use Ajax in following manner:

On selection of a value in one drop-down - fire an ajax request - sending the chosen value from the dropdown to the server. Now, modify the query at the server to select all the values excluding this value and then send the values back to the client. This way the user will be able to see the values excluding the chosen one.

Hope this helps.

Mysql Tutorials

Mysql Howto..

How to get the number of decimal positions of a mysql table column

How to get the Smallest value from the Top 3 rows of the table in mysql

How do you create a database in mySQL Workbench that you can connect to with php?

How To Calculate values

How to concatenate strings before GROUP_CONCAT

How are regular and composite indexes implemented in RDBs?

How to find in MySQL the nearest (time wise) entry to a given date when only given year or only year and month?

mysql - how to include zeros in the count in only one single query

MySQL how to fill missing hours/dates in range?

How to get the information from table to another page using PHP and MYSQL?

How To Sort Like Hacker News

How to separate result values in different rows using Mysql query

How would I query a MySQL database, if given a huge list of data to try to find out the best suited results?

How to update MYSQL Database with data from Excel using ASP.NET?

How to check in batch script if “mysqldump” and “mysql” commands succeeded or not?

How to set max_allowed_packet in phpmyadmin?

How can I search the string X times in the DB? (MYSQL + PHP)

How to select rows from MySQL based on max value of a column + grouping

How can I return mysql columns from separate tables with one stored procedure?

How to save checkbox state and post values to page after user clicks next pagination page or on page refresh?

How do I specify to include a specific zip code in this MySQL query?

How to not likes many items in MySQL?

How to export mysql database

how to set up daily php functions or mysql queries

How should I arrange multiple tables in MySQL

How to loop inside javascript using php variable

How to parse 0000-00-00 00:00:00?

How to build a paypal donation system that gives points

Query 64 byte (char) hash in MySQL database takes several seconds. How to improve?

How can I query a column which has apostrophes in it with MySQLi