shekhar jadhav shekhar jadhav - 1 year ago 74
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 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 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 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> ';


PHP code is


global $con;

//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'];


//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);



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

echo "Not inserted";



Answer Source

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> '; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download