2

I have created a while loop

while($row = mysql_fetch_Array($result)) { 
  echo '<tr class="record"> <td></td><td>'
     . $row['company'] .'</td><td>'
     . $row['project'].'</td><td>'
     . $row['assignee']. '</td><td>'
     . $row['current_milestone'] . '</td><td>'
     . $date=date("d-m-Y", strtotime($row['start_date'])).'</td><td>' ..'</td><td>'
     . $row['next_milestone'] . '</td><td>'
     .   $date=date("d-m-Y", strtotime($row['next_date']))
     .'</td></tr>' ; 
}

This displays correctly but my question is:

How can the user select an individual row so he/she can edit it. I was hoping that the user can select which row they wanted and they would be taken to another screen where they can make the changes. I can figure out how to do the part where I can update the data. But it is the selecting the row and collecting correct data I dont understand how to do.

4

3 に答える 3

0

Embed an <a href='update.php?id=> somewhere in there which carries the record id to the update script as a parameter in the query string:

while($row = mysql_fetch_Array($result)) { 
  echo '<tr class="record"> <td></td><td>'
     // Insert a link with an id parameter.
     // Use whatever column value uniquely identifies your row
     . '<a href="update.php?id=' . $row['id'] . '"> Edit this! </a>' 
     // etc... 
     // etc...
     // etc...
     .'</td></tr>' ; 
}

And retrieve it on the update script via:

// Assuming an integer id was passed...
// if it is some other string value, escape and quote it accordingly.
$id = intval($_GET['id']);

And you will want to check that the user making the edit has permission to edit that record. In other words, verify that the record is owned by the user is logged in, since anyone could technically visit the URL with any id, whether or not it belongs to them. As in:

UPDATE yourtable SET col1 = 'whatever', col2 = 'whatever' WHERE id = $id AND user_id = 'logged_in_user'
于 2012-07-28T20:28:47.547 に答える
0
  1. Create some sort of primary key (a new field that will be different for each row/entry) in your database. Example: an ID field
  2. Create a separate page for updating that accepts a GET parameter id. Ex: update.php
  3. Validate and sanitize the id parameter (ensure it is a valid id, not an SQL injection attempt, and the user has the privileges to edit this id)
  4. Load info from database for that id. Ex: SELECT fields FROM table WHERE id = $id
  5. Display a form so user can edit the information.
  6. Validate form input and update the table. Ex: UPDATE table SET field1=newVal WHERE id = $ID
于 2012-07-28T20:30:44.550 に答える
0

First of all, you need to create a primary key in the table, suppose primary_key in this case, so as to access the particular row having a definite and unique ID.

Secondly, add one more column to your table:

while($row = mysql_fetch_Array($result)){
echo '<tr class="record"> <td></td><td>'
         . $row['company'] .'</td><td>'
         . $row['project'].'</td><td>'
         . $row['assignee']. '</td><td>'
         . $row['current_milestone'] . '</td><td>'
         . $date=date("d-m-Y", strtotime($row['start_date'])).'</td><td>' ..'</td><td>'
         . $row['next_milestone'] . '</td><td>'
         .   $date=date("d-m-Y", strtotime($row['next_date']))
         .'</td>' ; 
         ?>
         <td>
            <form action='update.php' method='post'>
               <input type="hidden" name="id" value="<?php echo $row['primary_key']; ?>">
               <input type="submit" value="Edit">
            </form>
         </td>
         <?php
         echo "</tr>";
    }
    ?>

And now you know which row's details you need to display in the form at update.php The code on update.php goes like:

<?php
$id = $_POST['id'];
$result = mysql_query("SELECT * FROM table WHERE primary_key = '$id'");
$info = mysql_fetch_array($result);
?>
<form action='target.php' method='post'>
   Company: <input type="text" name="company" value="<?php echo $info['company']; ?>">
   Project: <input type="text" name="project" value="<?php echo $info['project']; ?>">
   .
   .
   <input type="hidden" name="id" value="<?php echo $info['primary_key']; ?>">
   <input type="submit" value="Save Changes">
</form>

And then on target.php all you have to do is use the command:

mysql_query("UPDATE table SET company='".$_POST['company']."' AND project='".$_POST['project']."' WHERE primary_key='".$_POST['id']."');
于 2012-07-28T20:56:41.633 に答える