Samer El Gendy
Samer El Gendy

Reputation: 1683

accessing the corresponding data in a checkbox table

I want to create a simple form that displays data items from database and i created a corresponding checkbox column, howver the problem is that i want to access the data item corresponding to checked checkbox, i hope someone can help, that's my code:

<form action="" method="post">
        <p>
            <?php
        $con = mysql_connect('localhost', 'root', "");
        if (!$con) {
            die ('connection error');
        }
        else {
            mysql_select_db("db_name", $con);
            $result = mysql_query ('SELECT `name` FROM `fruit`');

                 echo '<table width="100%">
                <tr>
                    <td><b>Name</b></td>
                    <td><b>choose</b></td>
                </tr>';
        while($row = mysql_fetch_assoc($result)) {
            echo "
                <tr>
                    <td>{$row['name']}</td>
                    <td><input type='checkbox' name='fruit' /><br /></td></tr>";
            }
        }
        if(isset($_POST['fruit'])) {
            echo $row['name'];
        }
        ?>
        </p>
        <p><input type="submit" value="save & send invitations" /></p>
   </form>

Upvotes: 1

Views: 253

Answers (1)

Moustachio
Moustachio

Reputation: 184

If I understand you correctly, you simply need to replace this:

if(isset($_POST['fruit'])) {
        echo $row['name'];
    }

With something like:

if(isset($_POST['fruit'])) {
  $result = mysql_query ('SELECT `name` FROM `fruit` WHERE `name`='.$_POST['fruit']);
  while($row = mysql_fetch_assoc($result)) {
        echo $row['name'];
  }
}

Upvotes: 1

Related Questions