0

I have an HTML table with radio buttons. The table displays fine but I do not know how to get the selected value into a database table. I am using PHP and MYSQL database.

Below is the code for the table

<?php
$stmt = $dbh->prepare("SELECT m.member_id , m.username , m.email , g.permission FROM members m INNER JOIN members_groups q 
ON m.member_id = q.member_id INNER JOIN groups g on q.group_id = g.group_id ");
?>

<table>
    <caption>List data from mysql</caption>
    <tr>
      <th class="center"><strong>Name</strong></th>
      <th class="center"><strong>Email</strong></th>
      <th class="center"><strong>Admin</strong></th>
      <th class="center"><strong>Account Manager</strong></th>
      <th class="center"><strong>delete</strong></th>
    </tr>
  <?php
  if($stmt->execute()){
      // output data of each row
      while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
          <tr>
              <td class="center"><?php echo $rows['username']; ?></td>
              <td class="center"><?php echo $rows['email']; ?></td>
              <td class="center">
                    <input type='radio' id='admin' name=<?php echo $rows['username']; ?> value="admin" 
                        <?php echo ($rows['permission']== 31 )?'checked':'' ?>></input> 
              </td>
              <td class="center">
                    <input type='radio' id='ac_manager' name=<?php echo $rows['username']; ?> value="ac_maneger" 
                        <?php echo ($rows['permission']== 1 )?'checked':'' ?> ></input> </td>
              <td class="center" >
                    <a href="javascript:if(confirm('Are you sure you want to delete?'))
                        { location.href='update_account.php?id=<?php echo $rows['member_id']; ?>'; }">delete</a>
              </td>
          </tr>
          <?php
      }
  }
  ?> 
</table>

Note: I Am yet to create the update_account.php. Just want to figure out how to get the buttons value from the table and store in the permission column of the groups table.

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
faisal abdulai
  • 3,739
  • 8
  • 44
  • 66

1 Answers1

0

you can name the radio as array and you can use the $member->id as the array key member[1] member[2] as you can see bellow:

<form method="POST">
<table>
    <tr>
        <td><input type="radio" value="foo" name="member[1]"/>foo</td>
        <td><input type="radio" value="bar" name="member[1]"/>bar</td>
    </tr>
    <tr>
        <td><input type="radio" value="foo" name="member[2]"/>foo</td>
        <td><input type="radio" value="bar" name="member[2]"/>bar</td>
    </tr>
</table>
<button>submit</button>
</form>

when you post you'll get a member key in $_POST['member'][1] = 'foo';

Thiago Cordeiro
  • 589
  • 3
  • 9