doomli3721 2011-05-24 15:26
浏览 136
已采纳

使用复选框在列表中显示SQL数据

I am not sure how to go about doing this, but I have an SQL Database where I query for info. I would like to display the query results in a list on a webpage with a check-box at the beginning so the user can select which they record of info they would like to modify.

My database has over 20+ columns but I only want to display probably 5 columns of data pertaining to the query that I searched for. Currently I have the data being repopulated in text fields. But I would like to go a step further and get the data displayed in a horizontal list.

Basically something like this:

Client searches for an order in the database. Say there are 2 different orders matching the search criteria in the DB. I would like say 5 columns of data from the DB for those 2 orders displayed on a page with a check-box next to each, so the client can then select which order they want to modify.

I am not sure of the code used to display the sql data in a list with a check-box. Thanks!

  • 写回答

2条回答 默认 最新

  • duanchuaiwan0063 2011-05-24 15:57
    关注

    The code below demonstrates how to:

    1. Query your database
    2. Display it's output in a table with checkboxes beside each entry
    3. Submit selected checkboxes to a page
    4. Display selection when page reloads

    Beware of some parameters that you may need to modify, e.g. database name, username, password, name of the primary key ("id").

    # connect to mysql
    $db_link = mysql_connect('localhost', 'root', '');
    if (!$db_link)
        die('Cannot connect : ' . mysql_error());
    
    # select database
    $db_selected = mysql_select_db('test_db', $db_link);
    if (!$db_selected)
        die ('Cannot select database : ' . mysql_error());
    
    # execute search query
    $sql = 'SELECT * FROM `test_table` LIMIT 20';
    $result = mysql_query($sql);
    
    # check result
    if (!$result)
        die('Could not successfully run query: ' . mysql_error());
    
    # display returned data
    if (mysql_num_rows($result) > 0)
    {
        ?>
        <form action="" method="post">
        <table style="border: 1px solid black">
            <?php
                while ($row = mysql_fetch_assoc($result))
                {
                    echo '<tr><td>';
                    echo '<input type="checkbox" name="selected[]" value="'.$row['id'].'"/>';
                    echo '</td>';
                    foreach ($row as $key => $value)
                        echo '<td>'.htmlspecialchars($value).'</td>';
                    echo '</tr>';
                }
            ?>
        </table>
        <input type="submit"/>
        </form>
        <?php
    }
    else
        echo '<p>No data</p>';
    
    # free resources
    mysql_free_result($result);
    
    # display posted data
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?