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

时间:2012-02-23 11:52:35

标签: php mysql html-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>&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;
        }
?>

1 个答案:

答案 0 :(得分:1)

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

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

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

重要

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