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

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

我正在尝试使用此窗体显示数据库中的表。我不确定我做错了什么。我交换了“form”和“select”标记的位置,假设这就是修复方法。但是每次从下拉列表中选择任何内容时,页面都会用我正常的数据库创建重新加载。正如您所看到的,我正在用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( )

         ?>
bwleehnv

bwleehnv1#

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

<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而不是表单。

相关问题