-1

I am trying to use this form to display tables in my database. I am unsure what I am doing wrong. I swapped the positions of the "form" and "select" tags assuming that was the fix. But everytime anything from the drop-down is selected the page simple reloads with my normal database creation. As you can see I am populating my tables within php, my only issue is when I try and put that information into an HTML form it doesn't work. I can send you examples of what I mean it this will help, but I assume you get what I am trying to do.

   <!-- 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( )

         ?>
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
Rob Nelson
  • 11
  • 8
  • Why do you have onchange="this.form.submit()" on select element. You also have an input button that submits the form. isn't the intent of the form to let user select a value and then hit submit button ? – Ajanth Jun 05 '18 at 00:09
  • What does this have to do with phpMyAdmin? – ceejayoz Jun 05 '18 at 00:39
  • (Also, I hope you're not dropping/creating the MySQL tables on every pageview...) – ceejayoz Jun 05 '18 at 00:41
  • I'm just trying to use the HTML form to display tables from a database I created in phpmyadmin. I didn't put the database name up for obvious reasons. – Rob Nelson Jun 05 '18 at 00:41
  • None of this code has anything to do with phpMyAdmin. phpMyAdmin is simply a tool to edit a MySQL database. I'm changing your tag/title to `mysql` instead. – ceejayoz Jun 05 '18 at 00:56

1 Answers1

0

Your <select> has onchange="this.form.submit()", which tells JavaScript to submit the form whenever you make a selection. Simply remove this to prevent your form from automatically submitting. Also, you'll want your submission button to be outside of your <noscript> (which really isn't necessary at all).

Ultimately, you want your <form> to look like the following:

<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>

Assuming you want to POST data automatically to your server without any sort of user interaction, you should make use of AJAX instead of a form.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Well, I want the Tables to have the ability to be selected and the generated to the page. Say you select concert, I'd like the table to show up and give you the concert_id(PK), and concert. the previous code from @obsidian does the same thing I already had it doing your code formatting is, however, more appealing. – Rob Nelson Jun 05 '18 at 00:20
  • That's completely different. You'll need to post your data off to a database, then use that database to output the information using PHP. PHP is server-side code, so it cannot 'update' a page after the page has been loaded; you'd need JavaScript for that. What you'll want to do is make use of AJAX to POST the selections off to a PHP page that generates JSON output, then use JavaScript to query that page with GET, store the JSON output in a variable, then parse that JSON client-side. You could trigger all of that to run `onchange`. See [**this**](https://www.w3schools.com/js/js_json_php.asp). – Obsidian Age Jun 05 '18 at 00:24
  • I will upload all code, as I am already using PHP, I just wanted to pull the data with HTML into a form from my database I have already created. give me a view minutes to edit the code. Apologies for the confusion. – Rob Nelson Jun 05 '18 at 00:26