访问复选框表中的相应数据

时间:2012-04-18 22:19:01

标签: php javascript mysql html checkbox

我想创建一个显示来自数据库的数据项的简单表单,我创建了一个相应的复选框列,问题是我想要访问对应于复选复选框的数据项,我希望有人可以提供帮助, 这是我的代码:

<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>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只需要替换它:

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

有类似的东西:

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