1

I have two PHP pages:

  • The first one has a form that insert data in the database.
  • The second diplays all the data of the database.

How could I, conceptually, adding for each row a button "update", that allow me to change some of the value of that row (in my example the value of the 2 dropdownlist) and update this info in the database as well.

This is the code I was able to do looking in internet and I have 2 problems: First, is working only the second update button (so only the second row get updated). Second, the changes does not get reflected into the dropdowlist. (means that if I change the second row value the database get updated but not the dropdownlist).

Note that I implemented the two columns

<td>" . $row['status'] . "</td>
<td>" . $row['priority'] . "</td>

Only to check that the value in the database changes.

    <form method="post" action="job-status.php">
           <?php

    include("../includes/connection.php");

        if($link->connect_errno > 0){
            die('Unable to connect to database [' . $link->connect_error . ']');
        }

        if(isset($_POST['update'])) {
           $results = $link->query("UPDATE job SET status='$_POST[status]', 
        priority='$_POST[priority]' WHERE id='$_POST[hidden]'");

        }    
           $sql = "SELECT * from job";
        if(!$result = $link->query($sql)){
            die('There was an error running the query [' . $link->error . ']');
        }
        echo "
    <table class='table'>
        <thead>
            <tr>";
        /* Get field information for all columns */
        while ($finfo = $result->fetch_field()) {
            echo "
            <th>" . $finfo->name . "</th>";
        }
        echo "
            </tr>
        </thead>
        <tbody>";
        while($row = $result->fetch_assoc()){
       echo "<tr class='info'>

                    <td>" . $row['id'] . "</td> 
                    <td>" . $row['device'] . "</td>
                    <td>" . $row['model'] . "</td> 
                    <td>" . $row['problem'] . "</td>
                    <td><select class='form-control col-sm-10' id='status' name='status'>
                         <option value='new'>New</option>
                         <option value='progress'>Progress</option>
                         <option  value='wait'>Wait</option>
                         <option value='done'>Done</option>
                         <option value='close'>Close</option>
                   </select></td>

                    <td><select class='form-control col-sm-10' id='priority' name='priority'>
                     <option value='high'>High</option>
                         <option value='medium'>Medium</option>
                     <option  value='low'>Low</option>
                     </select></td>

                       <td>" . $row['status'] . "</td>
                       <td>" . $row['priority'] . "</td>

                   <input type=hidden name=hidden value=" . $row['id'] . ">

<td><button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>

<td> <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal'>Info</a></td>
                </tr>"; 
        } 
        echo "
        </tbody>  
</table>"; 
?>   
</form>

enter image description here

4
  • Conceptually? Have the edit button create a form submit which calls an send.php form which is located on your server. Obviously you will have to send ALL data to that link, not just the name. That php file validates the users desired edit, then pushes the update to the mySQL database. Upon submit, refresh the DB display page to the user (showing the updated data entry) You could also Ajax for this, but that's a bit tricky (and requires use of JavaScript) or other tools Commented Oct 31, 2015 at 3:34
  • @zipzit What tool besides submitting a form and javascript could be employed? Commented Oct 31, 2015 at 3:53
  • Look at Symfony and their database extraction layer, Doctrine..(note there are other tools like this...) or learn how the advanced JavaScript tools like Angular.js work. Note: these are not for the casual fix.. these are highly developed tools, that will take a bit of time to understand and implement. Commented Oct 31, 2015 at 4:02
  • I updated my question in order to be more specific on what I want to achive. Commented Nov 1, 2015 at 16:56

2 Answers 2

1

You can use another TD for each row of your table:

"<td>" . $row['name'] . '</td><td><a href="LINK">Edit</a>' . "</td>"

The LINK will be a link to your controller, in case you are sing a MVC pattern, and you can pass the id of que row you want to delete. I mean somethig like:

"<td>" . $row['name'] . "</td><td><a href='clientes/editar?id=" . $row['id'] . "'>Edit</a> . "</td>"

This way you can edit any row just by clicking a "Edit" link. Remember to implement in the controller the logic for updating the row with the specific ID. For this purpose you can use a ORM wich can help you with the database access.

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

3 Comments

Will it be the same if i want to edit only a single column? For expemple if I have a multiple dropdown menu with choices and a submit button on the side in order to send the change to the database and update it?
Using this way you won´t need your delete button anymore. You only click the EDIT link and your controller will take care of the rest. You have to make sure that the link will take you to the controller, of course.
I did some work, and update my question, could you give me any other hint?
0

I solve part of the problem with the code below. The only remaining problem is: the dropdownmenu is showing all the values plus the value that is stored in the database, so that value gets showed twice. (see pic)

 while($row = $result->fetch_assoc()){
                echo "<form action='' method=post>";
              echo "<tr class='info'>
           <input type=hidden name=hidden value=" . $row['id'] . ">
                        <td>" . $row['id'] . "</td> 
                        <td>" . $row['device'] . "</td>
                        <td>" . $row['model'] . "</td> 
                        <td>" . $row['problem'] . "</td>

     <td><select class='form-control col-sm-10' id='status' name='status'>

         <option value=". $row['status'] ." >" . $row['status'] . "</option>
                          <option value='new'>New</option>
                          <option value='progress'>Progress</option>
                          <option  value='wait'>Wait</option>
                          <option value='done'>Done</option>
                          <option value='close'>Close</option>
                                                                                                </select></td>

     <td><select class='form-control col-sm-10' id='priority' name='priority'>
     <option value=". $row['priority'] ." >" . $row['priority'] . "</option>
                                <option value='high'>High</option>
                                <option value='medium'>Medium</option>
                                <option  value='low'>Low</option>
    </select></td>


                           <td>" . $row['status'] . "</td>
                           <td>" . $row['priority'] . "</td>

     <td>   <button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>

      <td> <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal'>Info</a></td>
     </tr>";   echo"</form>";
            }     
            echo "
            </tbody>

        </table>";

enter image description here

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.