This question already has answers here:
Answer
Solution:
One way, which would also provide a more conventional user experience, is to use a group of radio buttons in the form instead of lots of separate buttons:
<form method="post">
<input type="radio" name="selection" value="13" />One<br/>
<input type="radio" name="selection" value="14" />Two <br/>
<input type="radio" name="selection" value="15" />Three<br/>
<input type="radio" name="selection" value="16" />Four<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Then in the PHP you can simply get the submitted value from one field:
<?php
if (isset($_POST['submit'])) { // check the form was submitted
$id = $_POST["selection"];
$query = "SELECT * FROM crud WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
echo "Name:" . $row['name'];
}
Answer
Solution:
HTML form
<form name="form" method="post">
<button type="submit" name="id" value="13">Name One</button>
<button type="submit" name="id" value="14">Name Two</button>
<button type="submit" name="id" value="15">Name Three</button>
<button type="submit" name="id" value="16">Name Four</button>
</form>
PHP
<?php
if(isset($_POST['id']) && $_POST['id'] > 0) {
$id = (int)$_POST['id'];
$query="SELECT * FROM crud WHERE id=?";
$stmt=$conn->prepare($query);
$stmt->bind_param("i",$id);
$stmt->execute();
$result=$stmt->get_result();
$row=$result->fetch_assoc();
echo "Name:".$row['name'];
} else {
echo 'Have no "id" ';
}
?>
Source