Gervin
Gervin

Reputation: 17

Tried to display data in <select> but nothing appears in database when submitted

if(isset($_POST['submit'])){
    $patientname = $_POST['patientname'];
    $task = $_POST['task'];
    $description = $_POST['description'];
    $status = $_POST['status'];
    $date = date('Y-m-d H:i:s');
    $query = $db ->query("INSERT INTO task VALUES('','$patientname', '$task', 
'$description', '$status', '$date')");


<form method="post">
  <div class="form-group">
  <label for="assignedto">Assigned To</label>
  <select class="form-control" name="patientname" required>
      <option>Select Patient</option>
      <?php 
      $stmt=$dbcon->prepare("SELECT * FROM users");
      $stmt->execute();
      while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
          extract($row);
          echo "<option value=''> $type $username $Lastname</optioin>";
      }

  ?
    </select>
    <button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>

I don't know whats the problem but when I submit this data to database it shows blank.I hope you can help me guys. that is the output in database.the whole code runs in the same php file

Upvotes: 0

Views: 43

Answers (2)

Dimitris Filippou
Dimitris Filippou

Reputation: 809

There are a lot of problems I see with your code.

First of all you don't have any values in your Option.

My guess is that you would need something like this:

echo "<option value='$username $Lastname'> $type $username $Lastname</option>";

Then you never submit task description or status so you should have them in your form to as <input> fields. Something like this:

<input type="text" name="task"></input>

Then you have some syntax problems like you never open and close php tags here

<?php
if(isset($_POST['submit'])){
$patientname = $_POST['patientname'];
$task = $_POST['task'];
$description = $_POST['description'];
$status = $_POST['status'];
$date = date('Y-m-d H:i:s');
$query = $db ->query("INSERT INTO task VALUES('','$patientname', '$task', 
'$description', '$status', '$date')");
?>

and later on you have only ? instead of ?>

and also </optioin> instead of </option>

And you never close the <div class="form-group"> with a </div>

Upvotes: 0

IncredibleHat
IncredibleHat

Reputation: 4104

Your options do not have any values set. The string in the value attribute is what comes in on the $_POST['patientname'] variable.

So you have:

echo "<option value=''> $type $username $Lastname</optioin>";

Change to like:

echo "<option value='$username $Lastname'> $type $username $Lastname</option>";
//                        ^-add the value you want                      ^- fixed typo

However you do have some other issues you will need to overcome, like SQL injection prevention, and also escaping data in your html so the html wont break if user names have anything in them that is considered html. For example, it would best to echo your options like this:

echo '<option value="'. htmlspecialchars($username) .'">'. htmlspecialchars("$type $username $Lastname") .'</option>';

Upvotes: 1

Related Questions