php - Inserting values from a drop down list into database -
i'm trying insert/update 2 values in database questiontext , type. questiontext being inserted upon adding row, , successful updated when in need to. i'm not successful in either inserting type nor updating it. help?
case 'addquiz': $sql = "select id,questiontext,type questioninfo order 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'] $rowid => $text) { $sql = "update questioninfo set questiontext = '$text', type = '$type' id = '$rowid'"; mysqli_query($con,$sql); } } if(isset($_post['addquestion'])) { $sql="insert `questioninfo` (`id`) values (null)"; mysqli_query($con,$sql); } break;
actually think bit confused basic life cycle of web page form on it.
the first time page loaded due click on menu or link , therefore there no data process. try , process users inputs if 1 of form's <input type='submit' ....>
buttons pressed.
so basic layout of code process form should :-
<?php if ( $_server["request_method"] == 'post' ) { // or 'get' // user has pressed submit button // check required fields present in $_post/$_get // check button pressed if more 1 button exists // database access/updates etc based on validated inputs // store error message in array example used // in main html generating phase // set flag or 2 in html generating phase know // flavor of page want user see // based on did. } // end of user input processing // generate html initial page ( no user input ) // or possibly tailor output depending upon // user entered , processed above // , flags set above control // screen should
if closely, script trying process data wont avilable in case 'addquiz':
because when button generate pressed , fields have data in them, wont running case running because button create in case cause case run entirely.
Comments
Post a Comment