Greetings,
I have a PHP table which lists the values of each columm for a row. One of the values is a tinybit boolean, and I am trying to let the user check and uncheck a checkbox (in an array), and subsequantly update the database with the value.
Table code:
<table class="filelist">
<tr class="head">
<td>Displayed</td>
<td>Location</td>
<td>Caption</td>
<td>Upload Date</td>
</tr>
<?php
$conn=mysql_connect($host,$user,$password) or die(mysql.error());
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM tblmedia ORDER BY mediaId";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
if ($num==0)
{?>
No media in database.
<?php
}
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $row['mediaId']; ?>" <?php
if ($row['showMedia']==1)
{?>
checked="<?php echo "checked";?>"
<?php
}
?> /></td>
<td><?php echo $row['location']; ?></td>
<td><?php echo $row['caption']; ?></td>
<td><?php echo $row['uploadDate']; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td><input type="submit" name="hide" value="Hide All Media" /></td>
<td><input type="submit" name="display" value="Display All Media" /></td>
<td><input type="submit" name="hideSel" value="Hide Selected Media" /></td>
</tr>
</table>
<input type="hidden" name="count" value="<?php echo $num; ?>" />
</form>
And the code to update the database:
if (isset($_POST['hideSel']))
{
$conn=mysql_connect($host,$user,$password) or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$list=$_POST['checkbox'];
foreach($list as $i=>$value)
{
if ($list[$i]==0)
{
$sql5="UPDATE tblmedia SET showMedia=0 WHERE mediaId='$i='";
mysql_query($sql5) or die(mysql_error());
}
}
}
So how do I only get boxes which have been checked/unchecked and update the database?
Cheers,