【发布时间】:2014-12-14 06:25:09
【问题描述】:
我正在尝试 $_POST 对以下 mysql 表的更新:
注意:有 3 个下拉列表由索引表 ID 动态填充:
- 状态
- 类别
- 访问
参考表
表 1:app_generalData
+----------+--------+--------------+-------------- ----+------+----------------+
| app_id | 表格 | status_id | category_id | 标签 | access_id |
+----------+--------+--------------+-------------- ----+------+----------------+
状态说明
请参阅下面的 print_r 测试。
- 已为 3 个下拉列表中的每一个填充已存储的选项(已选择)
- 表单正在 POST,但...仅文本输入。
print_r($_POST)
数组(
[MAX_FILE_SIZE] => 100000
[app_id] => 2
[标题] => 我的专区
[状态] =>
[类别] =>
[标签] => 我的新标签
[访问] =>
[更新] => 更新)
问题:
- “选择”新值时选项不会改变
- 选项未显示在 $_POST 中
- 生成的 UPDATE 将创建一个零 (0) 来代替所选选项的现有值。
期望的结果:
- $_POST 选择的选项值
填充选择标签之一的代码:
<?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);
?>
【问题讨论】:
-
value "' . $row_2["$column_row_id"] . '"应该是value="' . $row_2["$column_row_id"] . '",带有=。
标签: php post mysqli selectedvalue