Just use the value of the row's primary key as the value of the checkbox. When/if that value gets submitted back, use it your populate your sql statement.
This could make handling multi-column primary keys troublesome, so a single-column, surrogate key will likely be useful. Note that you must
still enforce the natural key with a UNIQUE constraint even if you use a surrogate key.
Here's a simple example:
PHP Code:
$db = pg_connect('dbname=mydb');
$sql = 'SELECT id, username, first_name, last_name FROM my_table';
$result = pg_query($db, $sql);
echo "<form action=\"my_url\" method=\"POST\">\n"
echo "<p>Select the users you want to delete:</p>\n";
echo "<ul>\n";
while ($row = pg_fetch_assoc($db, $result)) {
echo '<li><label><input type="checkbox" name="delete[]" value="',
$row['id'], '">', $row['username'], ' — ', $row['first_name'],
' ' , $row['last_name'], "</label></li>\n";
}
echo "</ul>\n";
echo "</form>\n";
Now to process the form submittal:
PHP Code:
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$sql = sprintf(
'delete from my_table where id in (%s)',
implode(', ', $_POST['delete'])
);
pg_query($db, $sql);
}
Note this doesn't do any input sanitization/validation or protect from SQL injection attacks, all of which you will want to do. But it's enough of an example to show you how to go about this.