0

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.

11
  • Are $select and $_POST['selected_client'] irrelevant elements? They're either undefined, or unshown. Commented Aug 11, 2014 at 2:25
  • Also, there was a comment about your inputs being outside your form tags; which was deleted. I don't know why. Commented Aug 11, 2014 at 2:27
  • $_POST['selected_client'] is from another drop down menu that I'm not using to insert data with at the moment. Commented Aug 11, 2014 at 2:27
  • Also the $select variable was what I used for my conditional statement if data is actually present within the 'taskname' table field. Is there a more efficient way of using this while statement to output any data that is found from the database? Commented Aug 11, 2014 at 2:31
  • 1
    Your query is also not valid -> "INSERT INTO tasks (taskhours) VALUES :posted_hours WHERE taskname = :posted_task ". You don't use WHERE in an INSERT unless you are selecting INSERT INTO tbl SELECT FROM tbl2 WHERE id=# Commented Aug 11, 2014 at 2:41

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Put it inside and it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.