在带有复选框的列表中显示 SQL 数据

发布于 2024-11-09 03:04:41 字数 344 浏览 0 评论 0原文

我不知道如何去做这件事,但我有一个 SQL 数据库,我可以在其中查询信息。我想在网页上的列表中显示查询结果,并在开头带有一个复选框,以便用户可以选择他们想要修改的信息记录。

我的数据库有超过 20 列,但我只想显示与我搜索的查询相关的大约 5 列数据。目前我已将数据重新填充到文本字段中。但我想更进一步,将数据显示在水平列表中。

基本上是这样的:

客户在数据库中搜索订单。假设数据库中有 2 个不同的订单符合搜索条件。我想说,这 2 个订单的数据库中的 5 列数据显示在页面上,每个订单旁边都有一个复选框,这样客户就可以选择他们想要修改的订单。

我不确定用于在带有复选框的列表中显示 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是一片海 2024-11-16 03:04:42

下面的代码演示了如何:

  1. 查询数据库
  2. 在每个条目旁边带有复选框的表中显示其输出
  3. 将选定的复选框提交到页面
  4. 重新加载页面时显示选择

注意您可能需要修改的一些参数,例如数据库名称、用户名、密码,主键的名称(“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>';

?>

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

?>
别忘他 2024-11-16 03:04:42

基本上是这样的:

$sql = "select primaryKey, field1, field2, ... FROM table"
$result = mysql_query($sql) or die(mysql_error());

echo <<<EOL;
<form ...>
<table>
<tr>
    <th></th>
    <th>Field 1</th>
    <th>Field 2</th>
    ...
</tr>

EOL;

while ($row = mysql_fetch_asssoc($result)) {
    echo <<<EOL
<tr>
    <td><input type="checkbox" name="primaryKey[]" value="{$row['primaryKey']}" /></td>
    <td>{$row['field1']}</td>
    <td>{$row['field2']}</td>
    ...
</tr>

EOL;
}

echo <<<EOL
</table>
<input type="submit" />
</form>

EOL;

如果你的意思是像 以及一系列所需的

Basically, something like this:

$sql = "select primaryKey, field1, field2, ... FROM table"
$result = mysql_query($sql) or die(mysql_error());

echo <<<EOL;
<form ...>
<table>
<tr>
    <th></th>
    <th>Field 1</th>
    <th>Field 2</th>
    ...
</tr>

EOL;

while ($row = mysql_fetch_asssoc($result)) {
    echo <<<EOL
<tr>
    <td><input type="checkbox" name="primaryKey[]" value="{$row['primaryKey']}" /></td>
    <td>{$row['field1']}</td>
    <td>{$row['field2']}</td>
    ...
</tr>

EOL;
}

echo <<<EOL
</table>
<input type="submit" />
</form>

EOL;

If you mean list as in <select>, then the basic logic works the same, just adjust the html to output a <select> and a series of <option>'s as desired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文