'Select option menu read from database and use it's values

<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());

$query = "SELECT name,id FROM categories ORDER BY ID DESC LIMIT  0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");?>

    <select name="categories">
<?php while ($row = mysql_fetch_array($result)){
?>
   <option value=" <?php $row['path']; ?> ">
     <?php echo $row['name']; ?>
    </option>
<?php
}
?>        
</select>?>

So this is select option menu and it's value I read from database, but when I try to Get the selected value i get only the first one.

<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());


   if(!empty($_POST['title']) && !empty($_POST['date']) && !empty($_POST['txt']) && !empty($_POST['image'])){
$TITLE=$_POST['title'];
$DATE=$_POST['date'];
$TXT=$_POST['txt'];
$IMAGE=$_POST['image'];
$CATEGORIES=$_POST['categories'];
echo $CATEGORIES;
$ANSWER=$_POST['main'];
$MAINPAGE=0;

Could you help me with an idea to get the selected option



Solution 1:[1]

EDIT:(reduces ur code)

<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());

$query = "SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT  0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>

<select name="categories">
<?php 
while ($row = mysql_fetch_array($result))
{
    echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
}
?>        
</select>

Solution 2:[2]

I would prefer doing it like this using mysqli

<?php
/* Database connection settings */
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'tnews2';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);

/* Your query */
$result = $mysqli->query("SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT  0,6";) or die($mysqli->error);
?>

Then add the elements to html like this:

<select name="categories">
    <option value="Select School">Select Shool</option>
    <?php
    while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['path'] . "'>'" . $row['name'] . "'</option>";
    }
    ?>        
</select>

Solution 3:[3]

<?php
  include("conect.php");
  if ($db) { 
     $res=mysql_query("select bolao_concurso,bolao_data from Bolao");
     if ($res) {
        $lin_inic=1;
        $lines=mysql_num_rows($res);
        echo "<select name='conc'>";
        echo "<p>YOUR CHOICE : ";
        while ($lin_inic<=$linesS) {
              $row=mysql_fetch_array($res);
              $concurso =$row['bolao_concurso'];
              $data_conc=$row['bolao_data'];
              echo "<option value='$concurso'>$concurso $data_conc</option>";
              $lin_inic=$lin_inic+1;
        }
        echo "</select>";
     }
   }
?>

Solution 4:[4]

I would recommend making sure that you order the fields in the SQL Query to match the order you want them in and then use:

$query = "SELECT path, name, id FROM categories ORDER BY ID DESC LIMIT  0,6";

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

Your code will then be much more reusable as a 'code snippet', all you need to do is write a new SQL Query

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 marc_s
Solution 3 Josue Gomes da Silva
Solution 4 karlingen