发布选择值时收到错误

时间:2016-12-28 21:07:55

标签: php html

我有一个小问题。

<select name="level_id">未发布。 所以我收到如下错误:Undefined index : level_id

究竟我该怎么办?

$sql1 = 'SELECT T_ABILITY.PK AS AB_PK,T_ABILITY.ABILITY_NAME AS AN,T_ABILITY_LEVEL.PK AS LE_PK,T_ABILITY_LEVEL.LEVEL_NAME AS LN
FROM T_USER_ABILITY_REL,T_ABILITY,T_ABILITY_LEVEL WHERE
T_USER_ABILITY_REL.ABILITY_FK = T_ABILITY.PK AND
T_USER_ABILITY_REL.ABILITY_LEVEL_FK = T_ABILITY_LEVEL.PK AND
T_USER_ABILITY_REL.USER_FK = '.$user_id.'
ORDER BY AN';
$stmt1 = oci_parse($conn, $sql1);
$r1    = oci_execute($stmt1);
while ($row1 = oci_fetch_array($stmt1, OCI_RETURN_NULLS + OCI_ASSOC)) {
  echo '<form method="post">';
  echo '<tr>';
  echo '<td>'.$row1["AN"].'</td>';
  echo '<input type="hidden" name="ability_id" value="'.$row1["AB_PK"].'"/>';
  echo '<td class="select-level">';
  $sql2  = 'SELECT PK,LEVEL_NAME FROM T_ABILITY_LEVEL ORDER BY LEVEL_ORDER';
  $stmt2 = oci_parse($conn, $sql2);
  $r2    = oci_execute($stmt2);
  echo '<select name="level_id" class="form-control selectpicker" data-container="body" data-live-search="true" data-size="5" title="Seviye Seçiniz">';
  while ($row2 = oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC)) {
    echo '<option '.($row2["PK"] == $row1["LE_PK"] ? 'selected="selected"' : "").' value="'.$row2["PK"].'">'.$row2["LEVEL_NAME"].'</option>';
  }
  echo '</select>';

  echo '<button type="submit" name="update-user-ability" class="btn btn-success">Güncelle</button>';
  echo '<button type="submit" name="delete-user-ability" class="btn btn-danger">Sil</button>';
  echo '</td>';
  echo '</tr>';
  echo '</form>';
}

提交以下部分

if (isset($_POST["update-user-ability"])) {

    $user_id    = $_GET["user_id"];
    $ability_id = $_POST["ability_id"];
    $level_id   = $_POST['level_id'];

2 个答案:

答案 0 :(得分:0)

这是因为你的select是空的(即oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC没有返回任何行。)

如果没有选项,则select不会向接收php脚本返回值。

在循环之前添加一个默认选项,以确保即使你的sql查询没有返回任何内容也会传递一些东西。

此脚本将返回一个空帖子:

<html>
<body>
<form method="post">
    <select name="select"></select>
    <input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(0) { } 

虽然这个定义了值12:

<html>
<body>
<form method="post">
    <select name="select">
        <option value="12">12</option>
    </select>
    <input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(1) { ["select"]=> string(2) "12" } 

答案 1 :(得分:-1)

可能是level_id的值不会发送,因为它是<select>。不确定,但可能只有<input />值被正确发布。

您可以使用<input type="hidden" name="level_id_val" />并在每次更改<select name="level_id">的值时使用JavaScript更新其值(使用.onchange)。

然后,在PHP中,使用该字段而不是select:$level_id = $_POST['level_id_val'];