POST在php中选择的值


POST selected value in php

我正在尝试$_POST更新以下mysql表:
注意:有3个下拉列表由索引表id动态填充:

  • 状态
  • 类别
  • 访问

引用的表

表1:app_generalData
+----------+--------+--------------+------------------+------+----------------+
|app_id||status_id|category_id|标签||access_id
+----------+--------+--------------+------------------+------+----------------+

状态说明

请参阅下面的print_r测试。

  • 存储的选项(选定)将填充3个下拉列表中的每一个
  • 形式是张贴,但是文本输入

打印_r($_POST)

数组(
[MAX_FILE_SIZE]=>100000
[app_id]=>2
[title]=>我的区域
[状态]=>
[类别]=>
[tags]=>我的新标签
[访问]=>
[更新]=>更新)

问题:

  1. 当"选择"新值时,选项不会更改
  2. 选项未显示在$_POST中
  3. 得到的UPDATE为所选选项创建了一个零(0)来代替我现有的值

期望结果:

  1. $_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); 
?>

select是一个布尔属性

不是CCD_ 1而是CCD_。

只有<option value="product_1" selected>pizza</option>

请参阅部分示例:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select


形式是张贴,但是。。。仅文本输入。

提交表单后,请检查浏览器中的"网络"选项卡。查看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
};