HTML下拉菜单不会生成选定的MySQL表格?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (114)

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

         ?>
提问于
用户回答回答于

你的<select>has 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>

热门问答

springboot项目启动报错找不到ConfigurationPropertiesBean?

添加依赖:https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-context/2.2.0.RELEASE <!-- https://mvnrepository.com/artifac...... 展开详请

腾讯加固助手不能下载了吗?下载地址在哪里?MAC可以下载吗?我看只能在网页上加固,自己签名。?

腾讯云@移动安全

腾讯 · 移动开发工程师 (已认证)

腾讯云移动安全前端开发
推荐

乐固客户端已下线,可以使用移动应用在线加固https://console.cloud.tencent.com/ms/reinforce/list

可以自行签名打渠道包。

使用jenkins配合命令进行加固,在加固过程中提示40171,起码50%的概率出现?

whileideath

腾讯 · 运营开发工程师 (已认证)

web互助开发群:953701926,禁止广告,招聘行为。
推荐
内部返回数据解析失败(查毒) 指的是在apk进行加固之前会进行病毒检测,如果检测出现病毒,违法等应用将拒绝加固。 目前是该引擎存在故障。 ... 展开详请

节点数量如何计算?

吴楠NancyWU

腾讯云 · 高级产品经理 (已认证)

腾讯云区块链产品经理,与区块链一同成长,欢迎交流
推荐

建议网络测试可使用一个组1个节点配置,根据组织多少来定测试规模;企业应用以1个组织2个节点为基础起步,保持组织内的高可用,节点数量扩展根据组织数量和交易使用量增加,一个区块链网络内建议最多不超过100个节点。谢谢您的提问

腾讯云物联网设备端 C-SDK中coap sample运行失败getaddrinfo error?

DylanRichard

腾讯 · 产品经理 (已认证)

万物互联的时代,欢迎来到IoT的世界
推荐已采纳

用coap必须打开TLS,现在云端只支持DTLS的coap连接

关于云直播的几个问题,望大佬解惑?

学生路人
推荐

1 海外单独计费

2 地址都是自己算的 可以变也可以不变

3 这个接口是拉流转推的 ,和播放不是一个东西哈 。

扫码关注云+社区

领取腾讯云代金券