I am generating a list of buttons with the id being a value extracted from a database. Then I want to take this id and display. But, when I try to do that, I get an error. Can some one tell me what I’m doing wrong here.
Button generation
//This function will display items
function showItems($sqlString)
{
$result = mysqli_query($this->connectToDb(), $sqlString);
//var_dump($result);
if(mysqli_num_rows($result) > 0)
{
foreach ($result as $row)
{
echo "<div class='div_item'>";
echo "<img src='../images/uploads/".$row['It_image']."' style='width:200px;height:150px;'>"."<br>";
echo "IT CODE: ".$row['It_code']."<br>";
echo "ITEM: ".$row['It_name']."<br>";
echo "DESC: ".$row['It_desc']."<br>";
echo "QTY AVAIL: ".$row['qty']."<br>";
echo "PRICE: ".$row['price']."<br>";
echo "<form enctype='multipart/form-data' action='' method='POST'>";
echo "<button type='submit' name='id' id='".$row['It_code']."'>Buy</button>";
echo "</form>";
echo "</div>";
}
}
}
Displaying the ID when the respective button is clicked
//get id of the button
if (isset($_POST['id']))
{
echo $_GET['id'];
}
Error
2
Answers
Try to
var_dump($result)
, the error message tells us it can’t find the key "id". So var_dump could give us more information about what is missing.It could be because the form method is post, that data will be stored in the
$_POST['id']
and not$_GET['id']
Pass it inside value value=’".$row[‘It_code’]."’