Hi,
I currently have a small area of my site that allows a user to edit which logos are displayed on the Home Page.
It almost all works. You check the checkbox and submit and the SQL Table is updated. However, if the box is already checked and you want to uncheck (set visible = ‘N’) it isn’t possible.
Can someone help me out with this?
This is my sample code below that I’m using.
[U]Basically when a checkbox is unchecked, I want to set visible = ‘N’
[code=php]
if (isset($_POST[‘submitted’])) {
if(is_array($_POST[‘current’])) // check whether any of the checkboxes has been ticked, if not – no need to perform the update
{
$id_in = implode(‘,’, $_POST[‘current’]); // from all the boxes that have been ticked, create a string that looks like: 1,2,3,4 so it can fit into MySQL’s IN() function
mysql_query(“update footer_logos SET visible = ‘Y’ where id IN($id_in)”); // run ONE query and update all the records that are specified by the checkboxes.
}
}
$query1 = “SELECT * FROM footer_logos”;
$result1 = @mysql_query($query1); // Run the Query.
echo ‘<p><b>Edit “Footer Logos”</b></p>’;
echo $_POST[‘current’];
echo ‘<form name=”form1″ method=”POST” action=”update_logos.php” class=”book”>’;
echo ‘<table width=”550″ border=”0″ style=”padding:5px”>’;
echo ‘<tr style=”background-color:#eaeaea;”>
<td>Visible</td>
<td>Image</td>
<td>Link</td>
<td>ALT Text</td>
</tr>’;
while ($row1 = mysql_fetch_assoc($result1)) { // use assoc, not array
if ($row1[‘visible’]==’Y’)
$checked=”checked”;
else
$checked=””;
// use id to name each one differently
echo “<tr>
<td><input type=’checkbox’ name=’current[{$row1[‘id’]}]’ id=’current_{$row1[‘id’]}’ value='{$row1[‘id’]}’ class=’maillistadd’ {$checked} /></td>
<td><img src=’../images/{$row1[‘src’]}’ /></td><td>{$row1[‘link’]}</td><td>{$row1[‘alt’]}</td>
</tr>”;
}
echo ‘</table>’;
?>
<br>
<input name=”submit” type=”submit” class=”submit” value=”Update the ‘Logos’ box”>
<br>
<br>
<input type=”hidden” value=”form1″>
<input type=”hidden” name=”submitted” value=”TRUE” />
</p>
</form>
<hr size=”2″ />
Many thanks!