Maarten wrote:
I want to be able to select all the series that are in table2 from the db
and put the output on a page with checkboxes next to each serie.
If the user that I am modifiing has access it should be checked and if the
user hasn't got access to this album it should be unchecked. As the
siteadministrator I want to be able to check or uncheck each line and put
that back in the database.
This isn't just a SQL question, this is a web programming question.
Fetching the data is relatively easy:
SELECT A.albumname, IF(C.user IS NULL, 1, 0) AS is_checked
FROM albumstable AS A LEFT OUTER JOIN connection AS C
ON (A.albumname = C.album AND C.user = ?);
You need to output an HTML form with a list of the albums, and a
checkbox, checked if "is_checked" is 1.
<FORM>
<INPUT TYPE=hidden NAME=user VALUE=$user>
Loop:
$albumname
<INPUT TYPE=checkbox NAME="album_${albumname}"
VALUE=$is_checked>
</FORM>
In your code that processes the form, you need to figure out how to tell
the difference between old data and new data.
One method would be to delete _all_ the rows in Table3 for that user,
and then insert new rows for the checked album values found in the form.
$user = input parameter "user"
DELETE FROM connection WHERE user = $user;
Loop over input parameters matching "album_*":
if value of parameter is 0 (not checked), skip to next in loop.
$album = name of parameter, removing "album_" prefix.
INSERT INTO connection (user, album) VALUES ($user, $album);
It is recommended to do this series of statements in a transaction (if
you use InnoDB tables), so that if the INSERT fails for any reason, then
the DELETE should be rolled back.
Regards,
Bill K.