使用 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>  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;
}
?>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个好的起点是阅读有关如何从数据库中选择信息的信息。对于 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 MySQLSELECT
documentation can be found herehttp://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.