按提交按钮后显示数据库条目

时间:2012-06-16 13:01:00

标签: php mysql html-table

我有一个页面,我搜索作者写的书(基于我2个月前的作业的基本搜索)。我从下拉框中选择作者,然后按“提交”按钮后,结果应该出现。

这是页面的代码:

<?php

include ("includes/connections.php");

if($_POST)
{  
  if (!isset($_POST["authors"])){
          header("Location: searchAuthor.php");     
          exit;
  }

  foreach ($_POST["authors"] as $author) 
  {
      ?????????
  }
}
?>

<?php include ("includes/connections.php");
function dropdown($intIdField, $strfNameField, $strlNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
   echo "<select name=\"{$strNameOrdinal}[]\">\n";

   echo "<option value=\"NULL\">Select Value</option>\n";

   $strQuery = "SELECT $intIdField, $strfNameField, $strlNameField
               FROM $strTableName
               ORDER BY $strOrderField $strMethod";

   $rsrcResult = mysql_query($strQuery) or die(mysql_error());

   while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
      $strA = $arrayRow["$intIdField"];
      $strB = $arrayRow["$strlNameField"] . " " . $arrayRow["$strfNameField"];    
      echo "<option value=\"$strA\">$strB</option>\n";
   }

   echo "</select>";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Book Information</title>
<link href="back.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Search for Books of an Author</h1><table width="528" border="0" align="center">
    <tr>
      <td width="480"><span id="tip">*Hitting the "Search books of Author" button   without filling the fields with an asterisk will just reset the form</span></td>
    </tr>
    </table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="formBook">
  <table width="563" border="0" align="center">    
    <tr>
      <td style="text-align: right"><label for="authors">Select an Author*:</label></td>
      <td><?php dropdown("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" id="submit" value="Search books of Author" /></td>
    </tr>
    <tr>
      <td><div align="left"><img src="images/buttonleft.png" alt="previous" width="70" height="70" usemap="#Previous" border="0"></div></td>
      <td><div align="right"><img src="images/buttonright.png" alt="next" width="70" height="70" usemap="#Next" border="0">
        <map name="Previous">
            <area shape="circle" coords="35,35,33" href="addSubject.php">
        </map>
        <map name="Next">
          <area shape="circle" coords="35,35,33" href="addEdition.php">
        </map>
      </div></td>
    </tr>
  </table>
</form>

</body>
</html>

正如你所看到的,我把所有东西放在一张桌子里(方便这样的小东西)。我希望当我按下提交按钮时,选择在一个方法中输入的作者将显示查询的结果。查询将在foreach执行??????是。然后我希望查询的结果用于在我的表中显示(通过添加更多行并通过php函数在每行中插入一个结果)结果。

有没有办法用PHP做到这一点?我不知道如何使用Javascript只是PHP和HTML。即使我必须在另一个页面中插入查询结果并显示所有内容,我也可以。

我还没有编写查询。

实际上,foreach是在每个作者写过的书中加入一个变量。

1 个答案:

答案 0 :(得分:1)

这是一个例子。查询/字段是假的。

<?php

if (isset($_POST['Submit'])) {

   $strQuery = "SELECT 'field1', 'field2', 'field3'
           FROM $strTableName
           ORDER BY $strOrderField $strMethod";

$rsrcResult = mysql_query($strQuery) or die(mysql_error());

?>
<table>
<td> HEADER 1 </td> <td> HEADER 2 </td> <td> HEADER 3 </td>

<?php

 while ($row = mysql_fetch_array($rsrcResult) { 
 echo "<tr><td>".$row['field1']."</td><td>".$row['field2']."</td><td>".$row['field3']."</td>";

}

?>

</table>