Jeff P.
Jeff P.

Reputation: 3004

PHP MYSQL: Cannot insert data from drop down box and a text box

I am trying to insert data that the user submits from a drop down box and a text box, however this data is not being inserted into my Database Table.

The Drop Down and text box code:

echo    'Tasks: <select name="selected_task">
                  <option value=""> ---Select ---- </option><br><br><br><br>';

   /*
   Query code to retrieve options goes here and assign it to a variable named '$tasks'
   */


        while ($row_list = mysql_fetch_assoc($tasks))
          { 

        echo  '<option>' . $row_list['taskname'];
                    if ($row_list['taskname']==$select) { echo $row_list['taskname']; }
        echo '</option>';
                  }
        echo '</select>';  

        echo '<div id="task_hours"> 

  <form name="hours" method="post" action="login.php?action=hours"> 
    Hours: <input type="text" name="hours" value="" /><br />  
    <input type="submit" name="submit" value="Submit" /> 
  </form> 
</div>';

Database insertion code :

$posted_client = $_POST['selected_client'];
    $posted_task = $_POST['selected_task'];
    $conn_task = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $inserted_time = "INSERT INTO tasks (taskhours) VALUES :posted_hours WHERE taskname = :posted_task ";
    $sp = $conn_task ->prepare ( $inserted_time);
    $sp->bindValue(":posted_hours", $posted_hours, PDO::PARAM_STR); // Make sure you use PDO::PARAM_STR for decimals
    $sp->bindValue(":posted_task", $posted_task, PDO::PARAM_STR); 
    $sp->execute();
    $conn_task = null;
    echo "Your total time has been entered!<br>";
    echo "Your Total Hours: " . $posted_hours;

When the form appears and I click on the 'submit' button after entering data, no error message appears but the number of hours for the table field 'taskhours' is not being updated.

Upvotes: 0

Views: 377

Answers (2)

jems
jems

Reputation: 93

<Select>tag is a Element Of Form So when you use outside form then it doesn't work. so you have put the <Select> ('dropdown') is inside the <form> then your action page put below code and check you got the data or not in your post method.

<?php echo "<pre>"; print_r($_POST); ?>

here you got the all the form data.

Upvotes: 1

Xartrick
Xartrick

Reputation: 251

In this case, select tag isn't inside form one.

Put it inside and it will work.

Upvotes: 0

Related Questions