我正在尝试$ _POST更新到以下mysql表:
注意:索引表ID有<3>动态填充的3个下拉列表:
表1:app_generalData
+ ---------- + -------- + -------------- -------------- + ---- + ------ + ---------------- +
| app_id | 表格 | status_id | category_id | 标签 | access_id |
+ ---------- + -------- + -------------- -------------- + ---- + ------ + ---------------- +
请参阅下面的print_r测试。
阵列(
[MAX_FILE_SIZE] =&gt; 100000
[app_id] =&gt; 2
[title] =&gt; MyZone
[status] =&gt;
[category] =&gt;
[tags] =&gt;我的新标签
[access] =&gt;
[更新] =&gt;更新)
<?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);
?>
答案 0 :(得分:1)
select是一个布尔属性
不是$selected='selected="selected"';
而是$selected='selected';
。
只需<option value="product_1" selected>pizza</option>
。
请参阅选择示例:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
表单是POST,但是......只有文本输入。
提交表单后,请检查浏览器中的网络选项卡。 看一下POST请求发送。您选择的元素(下拉列表)被命名为“status”。 它应该填充。
换句话说:修复你的HTML。
删除$ kk而不是
$optionsList[$kk++] = '<option ' . $selected . ' value "' . $row_2["$column_row_id"] . '">' . ucwords($row_2["$column"]) . '</option>';
刚
$optionsList[] = '<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';
}
只是
echo implode($optionsList, "\n") . "\n";
无关,但删除并移动评论到顶部(如果..):
else {
// Action 'if' no [app_id] is appended in the url
};