MySQL Result in PHP/HTML Generated Table - Click Row To Edit?

Discussion in 'Web Design and Development' started by kolax, Jul 25, 2012.

  1. kolax macrumors G3

    Joined:
    Mar 20, 2007
    #1
    My website shows results from a MySQL database using PHP into an HTML table.

    What I want, at the end of each row, is an 'edit' link, which takes you to a page called edit.php and posts the variables of the row which you clicked edit on.

    This is my code so far:

    Code:
    $result = mysql_query("SELECT * FROM name_number_table");
    
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo '<td>' . $row['name'] . "</td>";
        echo '<td>' . $row['number'] . "</td>";
        [b]echo '<td><a href="edit.php">' . $row . "</td>";[/b]
        echo "</tr>";
    }
    echo "</table>";
    The bit in bold is what I need to get working. Right now, it works as a link to edit.php, but I need it to post the row variables too.

    Any ideas? Thanks.
     
  2. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #2
    How I'd go about it, assuming that each row has a unique 'id':
    PHP:
    <?php
    $result 
    mysql_query("SELECT * FROM name_number_table");

    while(
    $row mysql_fetch_array($result)): ?>
    <tr>
    <td><?=$row['name']?></td>
    <td><?=$row['number']?></td>
    <td><a href="edit.php?id=<?=$row['id']?>">Edit</a></td>
    </tr>
    <?php endwhile; ?>
    </table>
    Then in 'edit.php' you can use the row id from $_GET['id'] to query the database for the field values and populate your edit form. To me, this method is cleaner.

    However, if passing the field values to the form is the way you want to go, each edit link will need to contain all the field values in the query string. It can be kind of ugly...

    PHP:
    <?php
    while($row mysql_fetch_array($result)): ?>
    <tr>
    <td><?=$row['name']?></td>
    <td><?=$row['number']?></td>
    <td><a href="edit.php?name=<?=$row['name']?>&number=<?=$row['number']?>">Edit</a></td>
    </tr>
    <?php endwhile; ?>
    In edit.php the values can be gotten from $_GET['name'] and $_GET['number']

    I apologize if my use short open tags offends anyone...
     
  3. kolax thread starter macrumors G3

    Joined:
    Mar 20, 2007
  4. 618537 Guest

    Joined:
    Sep 21, 2011
    #4
    Remember SQL Injection Protection for all user generated variables.
     
  5. kolax, Jul 26, 2012
    Last edited: Jul 26, 2012

    kolax thread starter macrumors G3

    Joined:
    Mar 20, 2007
    #5
    My table is just blank now (it has the correct amount of rows, just no data).

    The only thing I've changed is instead of embedding the HTML code within PHP and using echo, I've embedded the PHP code like in your example with the HTML code.

    PHP:
    <?=$row['name']?>
    Is that definitely correct?


    EDIT:

    Got it working with:

    PHP:
    <?php echo $row['name']?>
     
  6. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #6
    Your server may not allow short tags. I probably shouldn't use them either, but I do anyway. What you did is definitely correct though.
     

Share This Page