简体   繁体   中英

Inserting values from a drop down list into database

I'm trying to insert/update two values in my database questiontext and type. questiontext is being inserted upon adding a row, and successful updated when in need to. However i'm not successful in either inserting the type nor updating it. Any help?

 case 'Addquiz':

         $sql = "SELECT id,questiontext,type FROM questioninfo ORDER BY type DESC ";

         $result = mysqli_query($con,$sql);
         $selectedtable  = "<form method='post' action=''>\n";
         $selectedtable .= "<table class='sortable'>\n<tr><th>Question</th><th>Type</th></tr>\n";

         while($row = mysqli_fetch_assoc($result)) { 

             $rowID = $row['id']; 
             $text = $row['questiontext'];
             $type = $row['type']; 

            $selectedtable .= "<tr>
<td><input type='text' name='QuestionText[$rowID]' value='$text'></td><td><select name='type[$rowID]'><option selected='selected'></option><option value='$type'>Performace</option><option value='$type'>Loyalty</option></select></td></tr>\n"; 

         }
         $selectedtable .= "</table>\n"; 
         $selectedtable .= "<input type='submit' name='submit' value='Update' style='width:80px; height:30px; text-align:center; padding:0px;'>\n";
         $selectedtable .= "<input type='submit' name='addquestion' value='Add Question' style='width:140px; height:30px; text-align:center; padding:0px;'>\n";
         $selectedtable .= "</form>\n";

         if(isset($_POST['submit'])) {   
             foreach($_POST['QuestionText'] as $rowID => $text) { 

                 $sql = "UPDATE questioninfo 
                         SET questiontext = '$text', 
                             type = '$type' 
                         WHERE id = '$rowID'"; 
                  mysqli_query($con,$sql);
             } 

          }
          if(isset($_POST['addquestion'])) {   
              $sql="INSERT INTO `questioninfo` (`ID`) VALUES (NULL)";
               mysqli_query($con,$sql);
        }


    break;

Actually I think you are a bit confused about the basic life cycle of a web page with a form on it.

The first time the page is loaded its probably due to a click on a menu or link and therefore there will be no data to process. You only try and process the users inputs if one of the form's <input type='submit' ....> buttons is pressed.

So the basic layout of the code to process a form should be something like this :-

<?php

    if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) { // or 'GET'

        // The user has pressed the submit button

        // Check that all required fields are present in $_POST/$_GET

        // check for which button was pressed if more than one button exists
        // Do any database access/updates etc based on validated inputs      
        // Store any error message in an array for example to be used 
        // in the main HTML generating phase

        // set a flag or 2 so in the HTML generating phase you know 
        // what flavor of page you want the user to see 
        // based on what the just did.
    } // end of user input processing


    // So now we generate the HTML for the initial page ( no user input )
    // or possibly tailor what we output depending upon
    // what the user entered and we processed above
    // and any flags we set above to control what this 
    // screen should look like

If you look closely, your script is trying to process data that wont actually be avilable in that case 'Addquiz': because when the button you generate is actually pressed and fields have data in them, it wont actually be running this case it will be running another because the button you create in this case will cause another case to run entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM