使用 SQL 语句生成的复选框获取和操作表中的值

发布于 2025-01-08 14:02:37 字数 2487 浏览 1 评论 0原文

我是编程新手,我找到了一个工作代码,它允许我根据查询生成一个表,该查询还包含结果表的每一行中的复选框。如何获取/检索通过复选框选择的行中的值?

这是生成表的代码:

<?php

        function SQLResultTable($Query)
        {
            $link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error());      //build MySQL Link
            mysql_select_db("myDB") or die('Could not select database');        //select database
            $Table = "";  //initialize table variable

            $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table

            $Result = mysql_query($Query); //Execute the query
            if(mysql_error())
            {
                $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
            }
            else
            {
                //Header Row with Field Names
                $NumFields = mysql_num_fields($Result);
                $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
                for ($i=0; $i < $NumFields; $i++)
                {
                    $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
                     if($i==$NumFields-1)
                     {
                     $Table.= "<th>&nbsp&nbspSelect&nbsp&nbsp</th>";
                     $Table.= "</tr>";
                     }
                }


                //Loop thru results
                $RowCt = 0; //Row Counter
                while($Row = mysql_fetch_assoc($Result))
                {

                    //Alternate colors for rows
                    if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
                    else $Style = "background-color: #FFFFFF;";

                    $Table.= "<tr style=\"$Style\">";
                    //Loop thru each field
                    foreach($Row as $field => $value)
                    {

                        $Table.= "<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp$value&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>";

                    }
                    //$Table.= "<td><input type='checkbox'></td>";
                    $Table.= "<td><input type='checkbox'></td>";
                    $Table.= "</tr>";
                }

            }
            $Table.= "</table>";

            return $Table;
        }
?>

I am new to programming and I found a working code which allows me to generate a table based from a query which also contains checkboxes in each row of the resulting table. How do I get/retrieve the values from the row/s which have been selected via the checkbox?

Here's the code which generates the table:

<?php

        function SQLResultTable($Query)
        {
            $link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error());      //build MySQL Link
            mysql_select_db("myDB") or die('Could not select database');        //select database
            $Table = "";  //initialize table variable

            $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table

            $Result = mysql_query($Query); //Execute the query
            if(mysql_error())
            {
                $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
            }
            else
            {
                //Header Row with Field Names
                $NumFields = mysql_num_fields($Result);
                $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
                for ($i=0; $i < $NumFields; $i++)
                {
                    $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
                     if($i==$NumFields-1)
                     {
                     $Table.= "<th>  Select  </th>";
                     $Table.= "</tr>";
                     }
                }


                //Loop thru results
                $RowCt = 0; //Row Counter
                while($Row = mysql_fetch_assoc($Result))
                {

                    //Alternate colors for rows
                    if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
                    else $Style = "background-color: #FFFFFF;";

                    $Table.= "<tr style=\"$Style\">";
                    //Loop thru each field
                    foreach($Row as $field => $value)
                    {

                        $Table.= "<td>      $value      </td>";

                    }
                    //$Table.= "<td><input type='checkbox'></td>";
                    $Table.= "<td><input type='checkbox'></td>";
                    $Table.= "</tr>";
                }

            }
            $Table.= "</table>";

            return $Table;
        }
?>

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

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

发布评论

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

评论(1

夜血缘 2025-01-15 14:02:37

一个好的起点是阅读有关如何从数据库中选择信息的信息。对于 SQL,通常这是使用 SELECT 语句来完成的。 MySQL SELECT 文档可以在这里找到

http: //dev.mysql.com/doc/refman/5.0/en/select.html

使用 Google,您应该能够找到有关如何在 MySQL 和 PHP 中使用 SELECT 的信息。一旦你尝试过了,如果你仍然无法弄清楚哪里出了问题,那么发布一个新问题,其中包含有关你的问题和示例代码的具体信息,然后我相信其他人将能够为您提供与该特定问题相关的建议。

重要

从安全角度来看,绝不在源代码示例中包含用户名或密码。只需输入占位符值即可。

A good starting point would be to read the information on how to select information from a database. With SQL generally this is done using SELECT statements. The MySQL SELECT documentation can be found here

http://dev.mysql.com/doc/refman/5.0/en/select.html

Using Google you should be able to find information regarding how to use a SELECT with MySQL and PHP. Once you've had a go, and if you still can't figure out where you are going wrong, then post up a new question with specific information as to your problem and example code, and then I'm sure others will be able to provide you with advice relating to that specific problem.

IMPORTANT

As a security point of view, NEVER include usernames or passwords in your source code examples. Just put in placeholder values instead.

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