0

I create a dropdown list to select values:

 <form action="." method="post" id="aligned">
    <input type="hidden" name="action" value="update_customer">
    <input type="hidden" name="customer_id" 
           value="<?php echo htmlspecialchars($customer['customerID']); ?>"> 


    <label>Country:</label>
    <select name="selected">
     <?php  

        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass ='';
        $db = 'tech_support';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass);
        if(!$conn)
            die('Could not connect: '. mysql_error());

        mysql_select_db($db);
        $selected= mysql_query("select * from countries where countryCode = '" .$customer['countryCode']. "'");
        $sql = mysql_query("select * from countries order by countryName");

        if($selectedrow = mysql_fetch_array($selected)){
             echo "<option selected value=\"VALUE\">" . $selectedrow['countryName']."</option>";
        }
         //echo "<select>";

        while ($row = mysql_fetch_array($sql)) {
        echo "<option value =\"VALUE\">". $row['countryName']."</option>";
        }
        //echo "</select>";
       ?> 
    </select><br>

In another php file as below, I was trying to store the selected value for Country, but nothing is stored, why?

<?php $country_name = $_POST["selected"];
echo $country_name;//it always print out 'VALUE'-I guess it means null.
?>
Dave
  • 3,073
  • 7
  • 20
  • 33
Jasmine
  • 17
  • 3
  • `echo " – Qirel Nov 25 '16 at 06:19
  • replace VALUE with any parameter or variable... `echo ""; }` – RJParikh Nov 25 '16 at 06:19
  • try this echo ""; – Deep Kakkar Nov 25 '16 at 06:20
  • `mysql_*` functions are deprecated since PHP 5.5 (and **removed entirely** in PHP 7) and you should [stop using them](http://stackoverflow.com/q/12859942) if you can. You should choose another API that allows you to use [prepared statements](http://stackoverflow.com/q/60174/) (which you *really should* when dealing with variables), like `mysqli_*` or PDO - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Qirel Nov 25 '16 at 06:27

1 Answers1

0

You should use

echo "<option value ='".$row['countryName']."'>". $row['countryName']."</option>";

And I want to add something, MySQL is deprecated in PHP5 and removed from PHP7. what is your PHP version. If you use PHP 5 or later use mysqli.

Kumar
  • 1,187
  • 18
  • 31
  • WELL, when I connect to localhost using mysqli, it works fine. But once connected to remote server mysqli doesn't work for queries somehow. I am using mysql php5. – Jasmine Dec 01 '16 at 00:50
  • Check your connection of mysqli on remote server. If it us fine then, you are wrong in sending query to server. What is PHP version of your remote server. – Kumar Dec 01 '16 at 11:23
  • I am using PHP 5.6. Mysqli connected to server since no error printed out. But the query result was not correctly presented. – Jasmine Dec 02 '16 at 02:20