0

I am trying to reflect the records from MySQL database using PHP. The code goes like this (Database is connected and selected)

Query.php -> This file reflect the distinct category(since I have multiple values of category) in select box from the database

<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from tbl_1 order by Category asc";
$query=mysql_query($sql);

while($row=mysql_fetch_array($query))
{

echo "<option value=$row[Name]>$row[Category]</option>";
}
?>
</select>
<input name="" type="submit" />
</form>

process.php-> This file take the option selected by user in query.php and reflect Name and district accordingly.

<?php

$myValue =$_POST['cat'];
echo $myValue;
$mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()"); 
if(mysql_num_rows($result2) == 0)
{ 
echo("<br/>No Records Found"); 
} 
ELSE 
{ 
echo "<table border='1'>"; 

//ECHO THE RECORDS FETCHED
   while($row = mysql_fetch_array($result2)) 
    { 
      echo "<tr>"; 
      echo "<td>" . $row['Station'] . "</td>"; 
      echo "<td>" . $row['Name'] . "</td>"; 

      echo "</tr>";

    }

 echo "</table>";  }

?>

PROBLEM-> On running the query.php I think the process.php does not recieve the selected option from query.php and hence I get "No Records Found". My database have the data. Can anyone tell me the mistake here...

user501582
  • 95
  • 1
  • 1
  • 7

1 Answers1

0

You are placing <option value=$row[Name]>$row[Category]</option> here value is $row[Name] not $row[Category] and selecting category

$mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'";  

try to place $row[Category] in option value like this

echo "<option value=".$row[Category].">".$row[Category]."</option>";
eggyal
  • 122,705
  • 18
  • 212
  • 237
Jobin
  • 8,238
  • 1
  • 33
  • 52
  • Got it resolved Instead of $mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'"; I used $mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '$myValue'"; – user501582 Apr 27 '12 at 17:13
  • But I have another issue. One of my category is "General Practitioner" and the array $myValue in process.php gives takes only "General". Am I missing any function here? – user501582 Apr 27 '12 at 17:23
  • query.php - displays category option only not set value. $row[Name] is empty. – Vaishu May 08 '12 at 04:34