yuki yuki - 7 months ago 34
PHP Question

Undefined index in php inside selected drop down

this is the undefined error that i got

the update is working. but after i clicked the submit button, the selected dropdown gave me this error.

$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...";
}
}


i put the php code and html on the same page..below is the 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>


i used the typeid to carry the values.

Answer Source

Try this:

<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>