0

I know somebody already answered this but for some reason their given syntax is not working for me. I am trying to fetch a radio button value from the database and display it in a radio button too. I watched many tutorials and red threads about this but it's still not working. Can you guys double check what is wrong with my code? Thank you.

This is how I fetch the value.

<label>2-3 meters
    <input type="radio" name="meter" value="2-3 Meters"
       <?php 
       if($ssh== "2-3 Meters"){
          echo "checked"; }
       ?>>
</label>

<label>1-2 meters
    <input type="radio" name="meter" value="1-2 Meters"
       <?php 
       if($ssh== "1-2 Meters"){
          echo "checked"; }
       ?>>
</label>

<label>< 1 meter
    <input type="radio" name="meter" value="< 1 Meter"
       <?php 
       if($ssh== "< 1 Meter"){
          echo "checked"; }
       ?>>
</label>

And this is my PHP

<?php

$con=mysqli_connect("localhost", "root", "", "db");

$edit=$_GET['id'];
$select="SELECT * FROM table WHERE id='$edit'";
$run=mysqli_query($con,$select);
$row=mysqli_fetch_array($run);

$id = "";
$ssh = "";

if(isset($_GET['ssh'])){
  $ssh = $_GET['ssh'];
}

?>
desteen
  • 187
  • 1
  • 1
  • 10

1 Answers1

1

Your $_GET['ssh'] is being passing through the URL and it is being encoded, for example:

Your given value 1-2 Meters (?ssh=1-2 Meters) will looks like 1-2%20Meters (?ssh=1-2%20Meters) when you retrieve it on the PHP by using $_GET['ssh']. Note the %20 characters intead the space, it happens because space is not a valid URL character. See: What's valid and what's not in a URI query?

So you should decode it before assign to $ssh variable.

if(isset($_GET['ssh'])){
  $ssh = urldecode($_GET['ssh']);
}

To fetch the values from the database and loop the radio buttons, it should be:

<?php
while($row=mysqli_fetch_array($run)){
?>
     <label><?php echo $row['title']; //whatever row name is on your database ?>
       <input type="radio" name="meter" value="<?php echo $row['title']; ?>"
       <?php 
       if($ssh == $row['title']){
          echo "checked"; }
       ?>>
     </label>
<?php } ?>

See docs: http://php.net/manual/en/mysqli-result.fetch-array.php

caiovisk
  • 3,667
  • 1
  • 12
  • 18
  • Thank you for telling me that. However, your reply doesn't answer my question :( – desteen Feb 21 '19 at 01:18
  • Sadly it only hid my radio buttons. Maybe I putted it in on a wrong place? I erased my code above and changed it with your code instead. – desteen Feb 21 '19 at 01:49
  • 1
    Do not erase your code... remove `$row=mysqli_fetch_array($run);` and put my code... also move `$ssh = ""; if(isset($_GET['ssh'])){ $ssh = $_GET['ssh']; }` before my code.... You need to provide more information, like PHP erros or so – caiovisk Feb 21 '19 at 01:56
  • Okay so it is now working but the problem is, it only prints the value and doesn't checked the radio button. I need the radio button to be checked too. @caiovisk – desteen Feb 21 '19 at 02:31
  • When you need to have the radio button checked?? again, provide more information – caiovisk Feb 21 '19 at 02:33
  • So there are 3 radio buttons. Say it's `rb1`, `rb2` and `rb3`. When the value from the db is `rb1` I want the radio button that have a value of `rb1` to be checked. – desteen Feb 21 '19 at 02:37
  • This line is doing the job: `>`... So are you passing value to `$_GET['ssh']` through the URL? Like yoururl.com?ssh=rb1 – caiovisk Feb 21 '19 at 02:39
  • I am trying to update a table using PHP and that is the reason why I want to fetch data from the db and display it. This is how my form looks like `
    `
    – desteen Feb 21 '19 at 02:44
  • You are using `method="post"` on your `form` and retrieving as `$_GET` on your `php`... Either you change your form `method` to `get` or change your `php` code to `$_POST['id']` instead of `$_GET['id']` – caiovisk Feb 21 '19 at 03:12
  • Will it still be able to update if the form method is `get`? Anyway, the button is still not checked :( I'm getting stressed. – desteen Feb 21 '19 at 03:18
  • Are you using $_GET['ssh'] or $_GET['id'] ?... You are not passing the `ssh` via `GET` – caiovisk Feb 21 '19 at 03:29