Ok, I know I'll probably get bashed for even asking this... its been about 5-6 years since I've touched PHP and MySql, so I think I may be overthinking all of this.
I have an HTML form, that is a series of checkboxes, and by clicking one, I would like it to set it's relative entry in the database to 0 or 1. When the update is complete, it returns to the form so I can change the settings again if needed.
The amount of entries (checkboxes) will be a static amount will be static, so I don't think a foreach loop will be necessary, I'm more or less trying to jog my memory on how to work with PHP, and I'm not having much luck reading a whole mess of answers scattered about SO that kinda have to deal with what I'm trying to do.
Any help will be greatly appreciated.
FORM:
<?php
require "config.php";
$db = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$query = "SELECT * FROM roster";
$result = mysql_query($query);
?>
<form action="update.php" method="POST">
<?php
while( $row = mysql_fetch_array($result) ){
$active = $row['active'];
$name = $row['name'];
$class = $row['class'];
$value = '0';
if ($active == '1'){
$value = '0';
} else {
$value = '1';
}
echo "<label class='switch'><input type='checkbox' name='".$class."' value='". $value."'";
if($active == '1'){
echo "checked='checked'";
}
echo "/><div class='slider'></div><span>" . $name . "</span></label>";
}
mysql_close();
?>
<input type="submit" class="submit" value="Update Active Roster">
</form>
Update.php (so far) - this is where I need help, I think
<?php
require "config.php";
$db = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$checkbox = $_POST['activate'];
for($i = 1; $i < sizeof($checkbox)+1; $i++){
$query = "UPDATE roster SET active='".$checkbox[$i]."' WHERE id='".$i."'";
echo "active='".$checkbox[$i]."' WHERE id='".$i."'";
mysql_query($query) or die(mysql_error());
}
?>
MySQL Database
id class name active
0 thisdude This Dude 0
1 thatguy That Guy 0
2 somechick Some Chick 0
Aucun commentaire:
Enregistrer un commentaire