2012-02-23 45 views
0

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

这里是产生该表的代码:

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

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

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

发现使用谷歌,你应该能够找到关于如何使用SELECT与MySQL和PHP的信息。一旦你走了,如果你仍然无法弄清楚你的问题出在哪里,那就提出一个新的问题,提供关于你的问题和示例代码的具体信息,然后我确信其他人将能够向您提供有关该特定问题的建议。

重要

作为安全的角度来看,NEVER包括在源代码的例子的用户名或密码。只需输入占位符值即可。