I am trying $_POST an UPDATE to the following mysql table:
Note: there are 3 dropdowns that are dynamically populated by indexed table id's:
Table1: app_generalData
+----------+--------+--------------+------------------+------+----------------+
| app_id | table | status_id | category_id | tags | access_id |
+----------+--------+--------------+------------------+------+----------------+
See the print_r test below.
Array (
[MAX_FILE_SIZE] => 100000
[app_id] => 2
[title] => MyZone
[status] =>
[category] =>
[tags] => MY New Tag
[access] =>
[update] => Update )
<?php
//connect to the database
require_once('connectvars.php');
// global variable for this populating this dropdown
$dropdown = "status";
$before_var = "app_";
$table= $before_var.$dropdown;
$after_var = "_title";
$column= $dropdown.$after_var;
$id_var= "_id";
$column_row_id= $dropdown.$id_var;
$optionsList = array();
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die ('Error connecting to MySQL server.');
echo '<select name="' . $dropdown . '"
type="text"
class=""
id="' . $dropdown . '">';
// See if we're viewing a selected app or are we creating a new app
if (isset($_GET['app_id'])) {
// print_r($_GET); // GET is Successful
// 'if' [app_id] is appended in the url
// STEP 1: Get the stored value of the SELECTED from mysql
// Get "selected item" (id) from app_generalData (table) with the "selected app" (id)
$option = "SELECT ".$column_row_id." FROM app_generalData WHERE app_id ='" . $_GET['app_id'] . "'";
// submit the select statement
// Get & store the value of "selected" <option>
$selected_option = mysqli_query($dbc, $option)
or die(mysql_error());
$row_1=mysqli_fetch_array($selected_option);
// STEP 2: Build the SELECT input
// Set the array of data to populate dropdown list <option>s
$options = "SELECT * FROM ".$table." ORDER BY ".$column_row_id."";
// NOTE: print_r($options)...SELECT access_title FROM app_access ORDER BY access_id
$selected_options = mysqli_query($dbc, $options)
or die(mysqli_error());
$kk = 0; //initialize kk
while($row_2 = mysqli_fetch_array($selected_options)) {
$selected ='';
if($row_1["$column_row_id"]==$row_2["$column_row_id"]) {
$selected='selected="selected"';
}
$optionsList[$kk++] ='<option ' . $selected . ' value "' . $row_2["$column_row_id"] . '">' . ucwords($row_2["$column"]) . '</option>';
}
// Echo the <option>s
$optionCount = count($optionsList);
for($i=0;$i<$optionCount;$i++) {
echo $optionsList[$i].'\n';
}
}
else {
// Action 'if' no [app_id] is appended in the url
};
// close the last <select> tag
echo '</select>';
// close the last database
mysqli_close($dbc);
?>
select is a boolean attribute
it's not $selected='selected="selected"';
but $selected='selected';
.
Just <option value="product_1" selected>pizza</option>
.
See select examples: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
The form is POST'ing, but...only text inputs.
when your form is submitted, check the network tab in your browser. take a look at the POST request send. your select formelement (dropdown) is named "status". it should be populated.
in other words: fix your html.
Remove $kk and instead of
$optionsList[$kk++] = '<option ' . $selected . ' value "' . $row_2["$column_row_id"] . '">' . ucwords($row_2["$column"]) . '</option>';
just
$optionsList[] = '<option ' . $selected . ' value="' . $row_2["$column_row_id"] . '">' . ucwords($row_2["$column"]) . '</option>';
Instead of:
// Echo the <option>s
$optionCount = count($optionsList);
for($i=0;$i<$optionCount;$i++) {
echo $optionsList[$i].'\n';
}
just
echo implode($optionsList, "\n") . "\n";
unrelated, but remove and move comment to the top (if..):
else {
// Action 'if' no [app_id] is appended in the url
};