For a basic project, I'm attempting to simulate a vending machine with a MySQL database backend. The problem I have is the following: when the user presses a button for what they would like to buy, the price of the item should be displayed. However, when I am attempting to query the price from the database using the variable based on the button pressed, it does not display the price. However, when I insert the name of the item into the query it does work.
I have generated a table with this.
echo "<tr><td>" . $snackName . "</td><td>" . $price . "
<a href = 'buying.php?varname= " . $snackName . "'><button>Buy</button></a>
</td></tr>";
Then on the buying page, I'm attempting to do this.
$snackChoice = $_GET['varname'];
$sql = ("SELECT `price` FROM `snacks` WHERE `snackName` = '$snackChoice'");
However, when displaying this, nothing displays on the page. But when I substitute something such as 'Mars Bar' then the price for it does display. What should I do to display the price based on what button the user will press?