【问题标题】:HTML Dropdown will not generate selected MySQL TablesHTML 下拉菜单不会生成选定的 MySQL 表
【发布时间】:2018-06-05 00:03:36
【问题描述】:

我正在尝试使用此表单来显示我的数据库中的表格。我不确定我做错了什么。假设这是修复,我交换了“表单”和“选择”标签的位置。但是每次从下拉列表中选择任何内容时,页面都会简单地重新加载我的正常数据库创建。 如您所见,我在 php 中填充我的表,我唯一的问题是当我尝试将该信息放入 HTML 表单时它不起作用。我可以向您发送我的意思的示例,这将有所帮助,但我假设您得到了我想要做的事情。

   <!-- Use JavaScript to automatically submit the selection -->
   <select name="lstDisplay" onchange="this.form.submit()">
      <option value="null">Select an item</option>
      <option value="concert">Concert</option>
      <option value="attendee">Atendee</option>
      <option value="venue">Venue</option>
   </select>
   <!-- set up alternative button in case JavaScript is not active -->
   <noscript>
      <input type="submit" name="btnSubmit" value="View the list" />
      <br /><br />
   </noscript>
   <!-- Use a hidden field to tell server if return visitor -->
   <input type="hidden" name="hidIsReturning" value="true" />
</form>

 // Check connection

        if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
        }

        // Start with a new database to start primary keys at 1


        $sql = "DROP DATABASE " . DATABASE_NAME;
        runQuery($sql, "DROP " . DATABASE_NAME, true);


        // Create database if it doesn't exist

        $sql = "CREATE DATABASE IF NOT EXISTS " . DATABASE_NAME;

        //if ($conn->query($sql) === TRUE) {
        //  echo "The database " . DATABASE_NAME . " exists or was created succesffuly!<br/>";
        //}

        //else {
        //  echo "Error creating database " . DATABASE_NAME . ": " . $conn->error;
        //  echo "<br/>";
        //}
        runQuery($sql, "Creating " . DATABASE_NAME, false);
        // Select the database

        $conn->select_db(DATABASE_NAME);

        /*
        --------------------------
          * Create the tables
        --------------------------
        */

        // Create Table: volunteer

        /*
                 --------------------------
                   * Create the tables
                 --------------------------
                 */

                 // Create Table: attendee
                 $sql = "CREATE TABLE IF NOT EXISTS attendee (
                         attendee_id    INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                         fName       VARCHAR(20),
                         lName        VARCHAR(20),
                         phone     VARCHAR(15),
                         email           VARCHAR(50)
                 )";
                 runQuery($sql, "Table:attendee", false);

        // Create Table: concert

                 $sql = "CREATE TABLE IF NOT EXISTS concert (
                         concert_id     INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                         concert        VARCHAR(20) NOT NULL
                 )";
                 runQuery($sql, "Table:concert", false);

        // Create Table: attendee_concert

        $sql = "CREATE TABLE IF NOT EXISTS attendee_concert (
                attendee_concert_id   INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                attendee_id           INT(6) UNSIGNED,
                concert_id            INT(6) UNSIGNED,
                paid                  TINYINT(1)
        )";
        runQuery($sql, "Table:attendee_concert", false);

         // Create Table: venue

                 $sql = "CREATE TABLE IF NOT EXISTS venue (
                         venue_id   INT(4) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                         venueName  VARCHAR(25)        
                 )";
                 runQuery($sql, "Table:venue", false);

        /*
                 --------------------------------------------
                 * Populate Tables Using Sample Data attendee
                 * This data will later be collected using a form.
                 --------------------------------------------
                 */
                 // Populate table: attendee

                 $attendeeArray = array(
                   array("Rob", "Nelson", "651-333-3030", "Rob@gmail.com"),
                   array("Matt", "Doe", "888-867-5309", "Matt@gmail.com"),
                   array("Tom", "Reynolds", "651-303-9090", "Tom@gmail.com"),
                   array("Jane", "Doe", "651-678-8901", "Jane@gmail.com"),
                   array("Emily", "Nelson", "612-234-5678", "Emily@gmail.com"),
                   array("Timmy", "Turner", "987-098-0987", "Timmy@gmail.com")
                 );

                 foreach($attendeeArray as $attendee) {
                   echo $attendee[0] . " " . $attendee[1] . "<br/>";
                   $sql = "INSERT INTO attendee (fName, lName, phone, email) "
                           . "VALUES ('" . $attendee[0] . "', '"
                           . $attendee[1] . "', '"
                           . $attendee[2] . "', '"
                           . $attendee[3] . "')";
                   runQuery($sql, "Record inserted for: " . $attendee[0], false);
                 }

         // Populate Table: concert

                 $concertArray = array("Brand New", "Thrice", "Daft Punk", "Kanye West",);

                 foreach($concertArray as $concert) {
                   $sql = "INSERT INTO concert (concert) " . "VALUES ('" . $concert . "')";
                   runQuery($sql, "New record insert $concert[0]", false);
                 }

        // Populate Table: attendee_concert

             $attendee_concertArray = array(
                array(1,1,1),
                array(2,2,1),
                array(3,3,1),
                array(4,3,1),
                array(5,3,1),
                array(6,4,1)
             );

        foreach ($attendee_concertArray as $attendee_concert) {
          $sql = "INSERT INTO attendee_concert (attendee_id, concert_id, paid) "
          . "VALUES ('" . $attendee_concert[0] . "', '"
                           . $attendee_concert[1] . "', '"
                           . $attendee_concert[2] . "')";
          runQuery($sql, "New record insert $attendee_concert[0]", false);
        }


        // Populate Table: venue

                 $venueArray = array("The Myth", "Target Field", "The Cabooze", "Blue Door Pub");

                 foreach ($venueArray as $venue) {
                   $sql = "INSERT INTO venue (venueName) "
                   . "VALUES ('" . $venue . "')";
                   runQuery($sql, "New record insert $venue[0]", true);
                 }

                 $sql = "SELECT * FROM attendee";
                 $result = $conn->query($sql);
                 displayResult($result, $sql); 
                 $conn->close(); 

                 function runQuery($sql, $msg, $echoSuccess) {

                   global $conn;

                   // run the query
                   if ($conn->query($sql) === TRUE) {
                      if($echoSuccess) {
                         echo $msg . " successful.<br/>";
                      }
                   } else {
                      echo "<strong>Error when: " . $msg . "</strong> using SQL: " . $sql . "<br/>" . $conn->error;
                   }

                 } // end of runQuery()


        function displayResult($result, $sql) {

          if ($result->num_rows > 0) {
            echo "<table border='1'>\n";
            // print headings (field names)
          $heading = $result->fetch_assoc( );
          echo "<tr>\n";
          // Print field names as table headings
          foreach($heading as $key=>$value){
             echo "<th>" . $key . "</th>\n";
          }
          echo "</tr>";
          // Print the values for the first row
          echo "<tr>";
          foreach($heading as $key=>$value){
             echo "<td>" . $value . "</td>\n";
          }
              // Output each record
              while($row = $result->fetch_assoc()) {
                //print_r($row);
                //echo "<br />";
                echo "<tr>\n";
                // print data
                foreach($row as $key=>$value) {
                   echo "<td>" . $value . "</td>\n";
                }
                echo "</tr>\n";
            }
            echo "</table>\n";
          // No results
          } else {
             echo "<strong>zero results using SQL: </strong>" . $sql;
          }


        } // end of displayResult( )

         ?>

【问题讨论】:

  • 为什么选择元素上有 onchange="this.form.submit()"。您还有一个提交表单的输入按钮。表单的意图不是让用户选择一个值然后点击提交按钮吗?
  • 这和phpMyAdmin有什么关系?
  • (另外,我希望您不会在每个页面浏览量上删除/创建 MySQL 表...)
  • 我只是想使用 HTML 表单来显示我在 phpmyadmin 中创建的数据库中的表格。出于明显的原因,我没有把数据库名称放在上面。
  • 这段代码与 phpMyAdmin 无关。 phpMyAdmin 只是一个编辑 MySQL 数据库的工具。我将您的标签/标题改为mysql

标签: php mysql select html.dropdownlistfor database-table


【解决方案1】:

您的&lt;select&gt;onchange="this.form.submit()",它告诉JavaScript 在您进行选择时提交表单。只需删除它以防止您的表单自动提交。此外,您希望您的提交按钮位于您的&lt;noscript&gt; 之外(这实际上根本没有必要)。

最终,您希望您的 &lt;form&gt; 如下所示:

<form>
  <select name="lstDisplay">
    <option value="null">Select an item</option>
    <option value="concert">Concert</option>
    <option value="attendee">Atendee</option>
    <option value="venue">Venue</option>
  </select>
  <input type="submit" name="btnSubmit" value="View the list" />
  <!-- Use a hidden field to tell server if return visitor -->
  <input type="hidden" name="hidIsReturning" value="true" />
</form>

假设您想自动将数据发布到您的服务器而无需任何类型的用户交互,您应该使用 AJAX 而不是表单。

【讨论】:

  • 好吧,我希望表格能够被选中并生成到页面。假设您选择了演唱会,我想显示该表并为您提供 Concert_id(PK) 和演唱会。来自@obsidian 的先前代码与我已经拥有它的代码格式相同,但是更吸引人。
  • 那完全不同。您需要将数据发布到数据库,然后使用该数据库使用 PHP 输出信息。 PHP 是服务器端代码,因此它不能在页面加载后“更新”页面;你需要 JavaScript。您要做的是使用 AJAX 将选择发布到生成 JSON 输出的 PHP 页面,然后使用 JavaScript 使用 GET 查询该页面,将 JSON 输出存储在变量中,然后解析该 JSON 客户端-边。你可以触发所有这些来运行onchange。见this
  • 我将上传所有代码,因为我已经在使用 PHP,我只是想将带有 HTML 的数据从我已经创建的数据库中提取到一个表单中。给我一个视图分钟来编辑代码。为混乱道歉。
猜你喜欢
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多