1

I use this code to select data from a database, each button retrieves names from a database. It works OK. But I am fairly new to PHP and MySQL and wonder if there is a better way to achieve the same result without using so many if isset statements.

<form name="form" method="post">
<input type="submit" name="button5" value="Name One" />
<input type="submit" name="button6" value="Name Two" />
<input type="submit" name="button7" value="Name Three" />
<input type="submit" name="button8" value="Name Four" />
    </form>
<?php
    
if(isset($_POST['button5'])){

$id =13;

}
    if(isset($_POST['button6'])){

$id =14;

}
    if(isset($_POST['button7'])){

$id =15;

}
    if(isset($_POST['button8'])){

$id =16;

}
    $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'];
    ?>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    well first of all, they should've been `elseif`'s because you know only one of them is going to be clicked each time, but maybe a `switch` statement? – Crimin4L Feb 16 '21 at 21:39
  • @Crimin4L Thanks for your advice, a very good point. –  Feb 17 '21 at 09:31

2 Answers2

2

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'];
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
ADyson
  • 57,178
  • 14
  • 51
  • 63
1

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" ';
}
?>
ExploitFate
  • 595
  • 2
  • 9
  • Thanks for your elegant solution, much better than my ugly attempt. For other newcomers to PHP looking at the solution, just add the missing } at the end. Thanks again –  Feb 17 '21 at 09:23