【问题标题】:accessing the corresponding data in a checkbox table访问复选框表中的相应数据
【发布时间】:2012-04-18 22:19:00
【问题描述】:

我想创建一个简单的表单来显示数据库中的数据项,并且我创建了一个相应的复选框列,但是问题是我想访问与选中的复选框对应的数据项,我希望有人能提供帮助, 这是我的代码:

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

【问题讨论】:

    标签: php javascript mysql html checkbox


    【解决方案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'];
      }
    }
    

    【讨论】:

    • 还要在条件范围之外声明数据库连接(最好在 dbconnect.php 文件中。)
    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2012-08-30
    • 2023-03-23
    相关资源
    最近更新 更多