1

i have this code.it is a dropdown list and a submit button. I have a query "SELECT * DISTINCT column_name FROM table_name" that has result 15 values

Now,i want to take those values of the query and dynamicly enter them on the option=..... field.Also the "NAME" section must be the same as values. example: IF value1="abcd"

<option value="abcd" >abcd</option>

<!DOCTYPE html>
<html>
<body>
<form  method="post" target=".....php">
<select name="exa" >
    <option value="value1" >NAME 1</option>
    <option value="value2" >NAME 2</option>
    <option value="value3" >NAME 3</option>
	<option value="value4" >NAME 4</option>
    <option value="value5" >NAME 5</option>
    <option value="value6" >NAME 6</option>
	<option value="value7" >NAME 7</option>
    <option value="value8" >NAME 8</option>
    <option value="value9" > NAME 9</option>
	<option value="value10" >NAME 10</option>
    <option value="value11" >NAME 11</option>
    <option value="value12" >NAME 12</option>
	<option value="value13" >NAME 13</option>
    <option value="value14" >NAME 14</option>
    <option value="value15" >NAME 15</option>
</select>

<form action=$value>
<input type="submit" value="GO!"  />
</form>
</body>
</html>

i've made this but didnt work

<!DOCTYPE html>
<html>
<body>
<form  method="post"  >
<select name="exa" >
<?php
 include_once "LOGIN TO DB SCRIPT";
 $query_ak='SELECT DISTINCT (column_name) FROM table_name';
 $result = mysql_query ($query_ak) or die (mysql_error);
 
    while ($row =  mysql_fetch_assoc($result)) {
    	}
	?>
	<option value = $row['ak_ex']> "$row['ak_ex']"</option>
 
<input type="submit" value="GO!"  name="go"/>
</select>
</form>
</body>
</html>

1
  • Note that Distict is not a function Commented Dec 13, 2014 at 17:44

2 Answers 2

1

You have made a lot of errors, first you've put the OPTION object outside the WHILE loop, then you put the submit button inside the SELECT object, in the end you write distinct as a function, the correct syntax is below.

I suggest you to use mysqli to avoid security problem and because mysql is going to be DEPRECATED. Use also include and not include_once because it requires extra work from PHP to render the file(Little difference but everything is welcome).

I've modified the code to use it, you find all the information to modify your script for working with mysqli at http://php.net/manual/it/book.mysqli.php

    <html>
    <body>
    <form  method="post"  >
    <?php
    // LOGIN TO DATABASE SCRIPT WRITTEN FOR MYSQLI
    $mysqli = new mysqli("localhost", "user", "password", "database");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .            $mysqli->connect_error;
     }
    // END OF LOGIN TO DB SCRIPT
    include DATABASE CONFIGURATION;
    $query_ak='SELECT DISTINCT column_name FROM table_name';
    $result = $mysqli->query($query_ak);
    ?>
    <select name="exa" >
        <?php
        while ($row =  mysqli_fetch_assoc($result)) {
            echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
        }
        ?>
    </select>
    <input type="submit" value="GO!"  name="go"/>
    </form>
    </body>
    </html>
Sign up to request clarification or add additional context in comments.

4 Comments

is it possible to take the selected $row['ak_ex'] and check its value in a different frame?
Yes, you have to call the MySQL script and check the value of row ak_ex with IF statements and print a result. If this resolved your problem please select an Accepted Answer or people will loose desire to answer your questions. Thanks
i am doing this and dont work $query_ak='SELECT DISTINCT (ak_ex) FROM table_name'; $result_ak = mysql_query ($query_ak) or die (mysql_error()); if (isset($_POST['go'])) { //elegxei an to while ($row = mysqli_fetch_assoc($result_ak)) { if ($_POST["exa"] = ".$row['ak_ex']." ) { $query= "SELECT * FROM table_name where ak_ex='.$row['ak_ex'].'"; $result= mysql_query($query) or die ('Query failed: ' . mysql_error());
Distinct is not a function, you've to use it like this: SELECT DISTINCT City FROM Customers;
1

You should placed <option>...</option> inside loop. Place submit button outside </select> tag. You also have syntax error. mysql_error should be mysql_error().

<!DOCTYPE html>
<html>
<body>
<form  method="post"  >
<?php
include_once "LOGIN TO DB SCRIPT";
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result = mysql_query ($query_ak) or die (mysql_error());
?>
<select name="exa" >
    <?php
    while ($row =  mysql_fetch_assoc($result)) {
        echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
    }
    ?>
</select>
<input type="submit" value="GO!"  name="go"/>
</form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.