Display one or two column only from database in PHP.
I have a MySQL database in PHPMyAdmin. There is a table and in that table, there are multiple columns like salary, salary advance, etc. I want to display when user select salary it only show salary column, that is all data in the salary column. Here is my code. But it doesn’t work. Please help.
<?Php
error_reporting(0);
include "config_1.php";
echo "<form method=post action='viewexpensebagnan.php' >
<select name='cn'>
<option value='None'>choose option </option>
<option value='salary'> salary </option>
<option value='sa'> sa </option>
<option value='house'> house </option>
<option value='gr'> gr </option>
<option value='bo'> bo </option>
<option value='goutam'> goutam </option>
</select>
<input type=submit ><br>
</form>";
if(value=='salary')
{
$query="select salary from expensebagnan";
$count=$dbo->prepare($query);
$count->execute();
$no=$count->rowCount();
echo "<table class='border' style='text-align:center;' width='100%' >";
echo "</td><td>";
echo "<tr><th>Salary</th></tr>";
foreach ($dbo->query($query) as $row){
echo "<tr><td>$row[salary]</td></tr>";
}
echo "</table>";
}
?>
For example: If I select salary from the dropdown and submit then it will show only salary column data.
2
Answers
There are so many issues with this code, but the thing why you cannot see the value from salary-key is that with this array-notation you need to wrap the array-access to curly-brackets, like this:
Using Ajax you can do it easily on change event of the drop-down list you can send the request with the value of drop down to the PHP code and PHP code will return you the values of the selected option.
Eg. Consider the following code (Put all code in one file, eg.index.php)
Now using jquery and ajax send the request to PHP as:
Your PHP code may look like this (getValues.php):