html下拉列表不会生成选定的mysql表

vjrehmav  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(437)

我正在尝试使用此窗体显示数据库中的表。我不确定我做错了什么。我交换了“form”和“select”标记的位置,假设这就是修复方法。但是每次从下拉列表中选择任何内容时,页面都会用我正常的数据库创建重新加载。正如您所看到的,我正在用php填充我的表,我唯一的问题是,当我尝试将这些信息放入html表单时,它不起作用。我可以给你我的意思,这将有帮助的例子,但我想你得到我想做的。

  1. <!-- Use JavaScript to automatically submit the selection -->
  2. <select name="lstDisplay" onchange="this.form.submit()">
  3. <option value="null">Select an item</option>
  4. <option value="concert">Concert</option>
  5. <option value="attendee">Atendee</option>
  6. <option value="venue">Venue</option>
  7. </select>
  8. <!-- set up alternative button in case JavaScript is not active -->
  9. <noscript>
  10. <input type="submit" name="btnSubmit" value="View the list" />
  11. <br /><br />
  12. </noscript>
  13. <!-- Use a hidden field to tell server if return visitor -->
  14. <input type="hidden" name="hidIsReturning" value="true" />
  15. </form>
  16. // Check connection
  17. if ($conn->connect_error) {
  18. die("Connection failed: " . $conn->connect_error);
  19. }
  20. // Start with a new database to start primary keys at 1
  21. $sql = "DROP DATABASE " . DATABASE_NAME;
  22. runQuery($sql, "DROP " . DATABASE_NAME, true);
  23. // Create database if it doesn't exist
  24. $sql = "CREATE DATABASE IF NOT EXISTS " . DATABASE_NAME;
  25. //if ($conn->query($sql) === TRUE) {
  26. // echo "The database " . DATABASE_NAME . " exists or was created succesffuly!<br/>";
  27. //}
  28. //else {
  29. // echo "Error creating database " . DATABASE_NAME . ": " . $conn->error;
  30. // echo "<br/>";
  31. //}
  32. runQuery($sql, "Creating " . DATABASE_NAME, false);
  33. // Select the database
  34. $conn->select_db(DATABASE_NAME);
  35. /*
  36. --------------------------
  37. * Create the tables
  38. --------------------------
  39. */
  40. // Create Table: volunteer
  41. /*
  42. --------------------------
  43. * Create the tables
  44. --------------------------
  45. */
  46. // Create Table: attendee
  47. $sql = "CREATE TABLE IF NOT EXISTS attendee (
  48. attendee_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  49. fName VARCHAR(20),
  50. lName VARCHAR(20),
  51. phone VARCHAR(15),
  52. email VARCHAR(50)
  53. )";
  54. runQuery($sql, "Table:attendee", false);
  55. // Create Table: concert
  56. $sql = "CREATE TABLE IF NOT EXISTS concert (
  57. concert_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  58. concert VARCHAR(20) NOT NULL
  59. )";
  60. runQuery($sql, "Table:concert", false);
  61. // Create Table: attendee_concert
  62. $sql = "CREATE TABLE IF NOT EXISTS attendee_concert (
  63. attendee_concert_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  64. attendee_id INT(6) UNSIGNED,
  65. concert_id INT(6) UNSIGNED,
  66. paid TINYINT(1)
  67. )";
  68. runQuery($sql, "Table:attendee_concert", false);
  69. // Create Table: venue
  70. $sql = "CREATE TABLE IF NOT EXISTS venue (
  71. venue_id INT(4) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  72. venueName VARCHAR(25)
  73. )";
  74. runQuery($sql, "Table:venue", false);
  75. /*
  76. --------------------------------------------
  77. * Populate Tables Using Sample Data attendee
  78. * This data will later be collected using a form.
  79. --------------------------------------------
  80. */
  81. // Populate table: attendee
  82. $attendeeArray = array(
  83. array("Rob", "Nelson", "651-333-3030", "Rob@gmail.com"),
  84. array("Matt", "Doe", "888-867-5309", "Matt@gmail.com"),
  85. array("Tom", "Reynolds", "651-303-9090", "Tom@gmail.com"),
  86. array("Jane", "Doe", "651-678-8901", "Jane@gmail.com"),
  87. array("Emily", "Nelson", "612-234-5678", "Emily@gmail.com"),
  88. array("Timmy", "Turner", "987-098-0987", "Timmy@gmail.com")
  89. );
  90. foreach($attendeeArray as $attendee) {
  91. echo $attendee[0] . " " . $attendee[1] . "<br/>";
  92. $sql = "INSERT INTO attendee (fName, lName, phone, email) "
  93. . "VALUES ('" . $attendee[0] . "', '"
  94. . $attendee[1] . "', '"
  95. . $attendee[2] . "', '"
  96. . $attendee[3] . "')";
  97. runQuery($sql, "Record inserted for: " . $attendee[0], false);
  98. }
  99. // Populate Table: concert
  100. $concertArray = array("Brand New", "Thrice", "Daft Punk", "Kanye West",);
  101. foreach($concertArray as $concert) {
  102. $sql = "INSERT INTO concert (concert) " . "VALUES ('" . $concert . "')";
  103. runQuery($sql, "New record insert $concert[0]", false);
  104. }
  105. // Populate Table: attendee_concert
  106. $attendee_concertArray = array(
  107. array(1,1,1),
  108. array(2,2,1),
  109. array(3,3,1),
  110. array(4,3,1),
  111. array(5,3,1),
  112. array(6,4,1)
  113. );
  114. foreach ($attendee_concertArray as $attendee_concert) {
  115. $sql = "INSERT INTO attendee_concert (attendee_id, concert_id, paid) "
  116. . "VALUES ('" . $attendee_concert[0] . "', '"
  117. . $attendee_concert[1] . "', '"
  118. . $attendee_concert[2] . "')";
  119. runQuery($sql, "New record insert $attendee_concert[0]", false);
  120. }
  121. // Populate Table: venue
  122. $venueArray = array("The Myth", "Target Field", "The Cabooze", "Blue Door Pub");
  123. foreach ($venueArray as $venue) {
  124. $sql = "INSERT INTO venue (venueName) "
  125. . "VALUES ('" . $venue . "')";
  126. runQuery($sql, "New record insert $venue[0]", true);
  127. }
  128. $sql = "SELECT * FROM attendee";
  129. $result = $conn->query($sql);
  130. displayResult($result, $sql);
  131. $conn->close();
  132. function runQuery($sql, $msg, $echoSuccess) {
  133. global $conn;
  134. // run the query
  135. if ($conn->query($sql) === TRUE) {
  136. if($echoSuccess) {
  137. echo $msg . " successful.<br/>";
  138. }
  139. } else {
  140. echo "<strong>Error when: " . $msg . "</strong> using SQL: " . $sql . "<br/>" . $conn->error;
  141. }
  142. } // end of runQuery()
  143. function displayResult($result, $sql) {
  144. if ($result->num_rows > 0) {
  145. echo "<table border='1'>\n";
  146. // print headings (field names)
  147. $heading = $result->fetch_assoc( );
  148. echo "<tr>\n";
  149. // Print field names as table headings
  150. foreach($heading as $key=>$value){
  151. echo "<th>" . $key . "</th>\n";
  152. }
  153. echo "</tr>";
  154. // Print the values for the first row
  155. echo "<tr>";
  156. foreach($heading as $key=>$value){
  157. echo "<td>" . $value . "</td>\n";
  158. }
  159. // Output each record
  160. while($row = $result->fetch_assoc()) {
  161. //print_r($row);
  162. //echo "<br />";
  163. echo "<tr>\n";
  164. // print data
  165. foreach($row as $key=>$value) {
  166. echo "<td>" . $value . "</td>\n";
  167. }
  168. echo "</tr>\n";
  169. }
  170. echo "</table>\n";
  171. // No results
  172. } else {
  173. echo "<strong>zero results using SQL: </strong>" . $sql;
  174. }
  175. } // end of displayResult( )
  176. ?>
bwleehnv

bwleehnv1#

你的 <select>onchange="this.form.submit()" ,它告诉javascript在进行选择时提交表单。只需删除此项即可防止表单自动提交。此外,您还希望提交按钮位于 <noscript> (其实根本没必要)。
最终,你想要你的 <form> 如下所示:

  1. <form>
  2. <select name="lstDisplay">
  3. <option value="null">Select an item</option>
  4. <option value="concert">Concert</option>
  5. <option value="attendee">Atendee</option>
  6. <option value="venue">Venue</option>
  7. </select>
  8. <input type="submit" name="btnSubmit" value="View the list" />
  9. <!-- Use a hidden field to tell server if return visitor -->
  10. <input type="hidden" name="hidIsReturning" value="true" />
  11. </form>

假设您希望在没有任何用户交互的情况下自动将数据发布到服务器,那么应该使用ajax而不是表单。

相关问题