Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kolax

macrumors G3
Original poster
Mar 20, 2007
9,181
115
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.
 
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...
 
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']?>
 
Last edited:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.