shekhar jadhav shekhar jadhav - 5 months ago 12
PHP Question

Select value is not inserting into database

I have admins who insert entries into database, other values are properly inserting into database but select option value is not inserting. Firstly admin loged in and his session start after that he add new entries to database.
Please help.

My html form is

<form method="post" action="" enctype="multipart/form-data" role="form">

<div class="col-md-4 form-group">
<label for="SubjectMaterialTitle">Subject Material Title</label>
<input type="text" name="sub_title" placeholder="Subject Material Title" class="form-control" required>
</div>

<div class="col-md-4 form-group">
<label for="Downloadurllink">Download url link</label>
<input type="text" name="sub_url" placeholder="Download url link" class="form-control" required>
</div>

<div class="col-md-4 form-group">
<label for="exampleInputFile">File input</label>
<input type="file" name="sub_file" id="exampleInputFile" required>
<p class="help-block">Upload Image/File upto 2MB size only</p>
</div>

<div class="col-md-4 form-group">
<label for="WritenBy">Writen By</label>
<select class="form-control" name="writen_by">
<?php if(isset($_SESSION['admin_email'])){
echo '<option value='. $_SESSION['admin_username'] .'>'. $_SESSION['admin_username'] .'</option> ';
}
?>
</select>
</div>

</form>


PHP code is

<?php

include("includes/config.php");
global $con;
if(isset($_POST['add_submat'])){

//getting the text data from the fields
$sub_title = $_POST['sub_title'];
$sub_url = $_POST['sub_url'];
$writen_by = $_POST['writen_by'];

//getting image from the fields
$sub_file = $_FILES['sub_file']['name'];
$sub_file_tmp = $_FILES['sub_file']['tmp_name'];

move_uploaded_file($sub_file_tmp,"sub-material-files/$sub_file");

//insert data into table
$insert_submat = "insert into m_study_material (sm_title,sm_downloadurl,sm_image,created_by) values ('$sub_title','$sub_url','$sub_file','$writen_by')";

echo $insert_submat;

$run_submat = mysqli_query($con, $insert_submat);

//error_reporting(E_ERROR);

if($run_submat){

echo "<script>alert('Inserted Successfully.')</script>";

}
else{
echo "Not inserted";
}


}

?>

Answer

You forgot to add quotes arround value, thats why values is being post properly.

  <?php if(isset($_SESSION['admin_email'])){ 
                                                echo '<option value="'. $_SESSION['admin_username'] .'">'. $_SESSION['admin_username'] .'</option> '; 
                                                } 
                                            ?>