-1

I am fairly new to PHP and SQL. I want to be able to assign a task to a certain user and the update that row in the database with the user's name assigned to that task. Here is my code:

<table border="0" width="1100px" style= "font-size: 12px" >
  <thead>
    <tr valign="top" align="left">
    <th height="20"></th>
      <th height="20">Customer</th>
      <th>Vehicle</th>
      <th>Appt Time</th>
      <th>Notes</th>
      <th>Assign</th>
      <th>Action</th>
      <tr><td valign="top" colspan="6"><hr><br></td></tr>
    </tr>
  </thead>
  <tbody>
    <?php
      while( $row = mysql_fetch_assoc( $result ) ){
        echo
        "<tr valign='center'>

          <td width='50'><b>{$row['id']}</td>

            <td width='220' height='70'><b>{$row['firstname']}
          </td>


          <td width='240'><b>{$row['car']}</b> <br>{$row['reg']}</td>
          <td width='170'>Monday<br>24 September<br>17:00</td>

          <td width='240'>{$row['notes']}<br><b>Status:</td>
          <td width='240'>
          <form action='bookings.php' method='post'>
          <select style='width:90px' class='reg' name='assign'required>
          <option value=''></option>
          <option value='User1'>User1</option>
          <option value='User2'>User2</option>
          <option value='User3'>User3</option>
          <option value='User4'>User4</option>
          <option value='User5'>User5</option>
         </select><input type='submit' value='>'  class='assignButton'/></form>
          </td><td>
          <button class='myButton'>Edit</button>
          </td>
          <tr><td colspan='6'><hr class='hrTitle'></td></tr>
        </tr>\n";
      }  
    ?>

  </tbody>
</table>

As you can see, I have a number of users that can be selected, I want to be able to assign that task to a user from the select list. Any help is much appreciated.

K.Wali
  • 11
  • 2

1 Answers1

-1

Try the following steps.

  1. Give a name to your submit button.

    <input type='submit' value='>' name='submit' class='assignButton'>
    
  2. Add a hidden input field to your form. Give it the value of the id of the current row. Give it a name. I chose id. This is very important so you can know which customer to edit. Please double check the value that you will set. From your code, I see it is $row['id'].

    <input type="hidden" name="id" value='$row["id"]''>
    
  3. In your PHP file bookings.php (assuming you have a connection to your database), process the submitted form.

    if(isset($_POST["submit"])){ //checks if the form was submitted
        $id = $_POST["id"]; //id of the customer
        $assign = $_POST["assign"]; //your selected value
        $query = "UPDATE table SET columnToModify = '$assign' WHERE id = '$id'";
        $result = $connection->query($query); //run the query
    }
    

Hope it helps.

imran.razak
  • 188
  • 1
  • 12
  • Thanks a lot, I have done everything you said but litterly nothing is happening. There are no errors but the row is not assigned to anyone. Any idea why this would happen? – K.Wali Mar 09 '17 at 10:35
  • Show me your code. The bookings.php file and the html that contains the form. – imran.razak Mar 09 '17 at 13:43