php里面未定义的索引选中了下拉列表

时间:2017-05-12 02:13:47

标签: php

this is the undefined error that i got

更新正在运行。但在我点击提交按钮后,所选的下拉列表给了我这个错误。

$row=array();
if (isset($_GET['typeid'])) {
$sql = "SELECT * FROM vehicletype WHERE id_vehicleType=" . $_GET['typeid'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
    }
    // update record

   if(isset($_POST['submit'])){
    $id = mysqli_real_escape_string($link,$_POST['idtype']);
    $type = mysqli_real_escape_string($link, $_POST['type']);
    $status = mysqli_real_escape_string($link, $_POST['status']);
    $update = mysqli_real_escape_string($link, $_SESSION['idinfostaf']);

    $result = mysqli_query($link, "UPDATE vehicletype SET vehicle_Type='$type', status_vehicleType='$status', updateby_vehicleType='$update' WHERE id_vehicleType=".$id);

    if ($result) {

        $success = "Record updated successfully!";
    }
    else {
       $error = "Error updating record...";
    }
}

我将php代码和html放在同一页面上。下面是html

<div class="form-group">
  <label>Choose Vehicle Type Status</label>
  <select class="form-control" name="status" required class="form-control" value="<?php if(isset($row['status_vehicleType'])){ echo $row['status_vehicleType'];} ?>">
   <option value="">Select Vehicle Type</option>
   <option
            value="1" <?php if ($row['status_vehicleType']==$_GET["typeid"]) { echo  'selected="selected"' ;} ?> >Enabled</option>
        <option
            value="0" <?php if ($row['status_vehicleType']== $_GET["typeid"]) { echo  'selected="selected"' ;} ?> >Disabled</option>
    </select>

    <hr>

<button type="submit" name="submit" class="btn btn-info">Submit </button>

<span class="text-success"><?php if (isset($success)) { echo $success; } ?></span>
<span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>

我使用typeid来传递值。

1 个答案:

答案 0 :(得分:0)

试试这个:

<select value="<?php if(isset($row['status_vehicleType'])){ echo $row['status_vehicleType'];} ?>">
   <option value="">Select Vehicle Type</option>
   <option value="1" <?php 
                        if(isset($row['status_vehicleBrand'])) {
                            if ($row['status_vehicleBrand']==$_GET["typeid"]) { 
                                echo 'Selected' ;
                            }
                        } ?> >Enabled</option>
   <option value="0" <?php 
                        if(isset($row['status_vehicleBrand'])) {
                            if ($row['status_vehicleBrand']==$_GET["typeid"]) { 
                                echo 'Selected' ;
                            }
                        } ?> >Disabled</option>
 </select>