Hi,
I've created a list of email addresses from my database and I want to add a checkbox next to each email address to be able to subscribe or unsubscribe them from a mailing list.
I've successfully created the list and checkboxes (checked if they're subscribed otherwise unchecked), but I'm stuck on how to create the correct array and process it.
My form looks something like this:
<form action="newsletter.php" method='post'>
<table class='admin'>
<tr>
<th>Name</th>
<th>Username</th>
<th>Email</th>
<th>Subscribed</th>
</tr>
<tr>
<td>berts company</td>
<td>bert</td>
<td>bert@bert.com</td>
<td><input type='checkbox' name='row[bert]' value='0' /></td>
</tr>
<tr>
<td>Richard<td>
<td>jetskirich</td>
<td>rich@test.com</td>
<td><input type='checkbox' name='row[jetskirich]' value='1' /></td></tr>
</table>
<input type='submit' value='Submit' />
</form>
I think i'm creating an associative array with the username (row[bert]) as the key and it's associated value (0 in this case as unsubscribed).
With this in mind here's my php code to insert the values into the database:
$row = $_POST['row'];
foreach ($row as $username => $newsletter) {
$sql = mysql_query("UPDATE users SET
newsletter = '$newsletter'
WHERE username = '$username'
");
}
I get an error message saying Warning: Invalid argument supplied for foreach() in newsletter.php on line x.
Can anyone point me in the right direction? Am I going about this the right way?
The second problem is that if i want to unsubscribe an email address, then when a checkbox is unchecked, will the value of 0 be passed or will it be ignored?
Thanks
Richard.