- Joined
- Jan 23, 2013
- Messages
- 695
- Reaction score
- 101
Okay so basically, I wanted to design a system where I could save/load checkboxes to the DB (using mysqli). I will be creating an item checklist for a game, and well, this is rather useful.
Here's the stuff.
saving(write.php)
loading(load.php)
and last but not least, index.php
Mind you I am VERY new to php. It took me a few hours to figure this out, so be kind with your vocabulary please.
Basically, I know there's a way to only make it update columns that were changed. Something like if (check[$i] == $checkb[$i]), or along those lines. I don't really know how to do it efficiently.
As of right now, this all works perfectly. checkb is an array that is filled in config.php
I would also like to use isLogged to check if the IP is already in the DB, if it's not, use a different query. But I can't get that to work for some odd reason.
Thank you for your help guys!
Here's the stuff.
saving(write.php)
Code:
<?phpinclude('config.php');
for ($i = 0;$i < sizeof($checkb);$i++) {
if (isset($_POST["Check".$i.""])){
$checkb[$i] = 'checked';
} else {
$checkb[$i] = '!checked';
}
}
for ($z = 0; $z < sizeof($checkb);$z++) {
$query = "UPDATE `test` SET info".$z." = '".$checkb[$z]."' WHERE ip = '" . $_SERVER['REMOTE_ADDR'] ."'";
if (!mysqli_query($con, $query)) {
die('Error: ' . mysqli_error($con));
}
}
echo "".sizeof($checkb)." records updated";
?>
Code:
<?phpinclude ('config.php');
if ($isLogged) {
for ($s = 0; $s < sizeof($checkb); $s++) {
$query = "SELECT * FROM `test` WHERE ip = '" . $_SERVER['REMOTE_ADDR'] ."'";
}
$result = mysqli_query($con, $query);
$h = 0;
while ($row = mysqli_fetch_array($result)) {
$isLogged = true;
for($g = 0; $g < sizeof($checkb); $g++) {
$checkb[$g] = $row['info'.$g.''];
}
}
}
?>
Code:
<?phpinclude('load.php');
?>
<form action="write.php" method="POST">
<input type=checkbox name='Check0' value=check0 <?php echo $checkb[0]?>>Check me!
<input type=checkbox name='Check1' value=check1 <?php echo $checkb[1]?>>Check me!
<input type=checkbox name='Check2' value=check1 <?php echo $checkb[2]?>>Check me!
<input type=submit value="ok"></form>
Mind you I am VERY new to php. It took me a few hours to figure this out, so be kind with your vocabulary please.
Basically, I know there's a way to only make it update columns that were changed. Something like if (check[$i] == $checkb[$i]), or along those lines. I don't really know how to do it efficiently.
As of right now, this all works perfectly. checkb is an array that is filled in config.php
PHP:
//global variables$size = 3;$checkb = array();$checkb = array_fill(0, $size, '!checked');$isLogged = true;
I would also like to use isLogged to check if the IP is already in the DB, if it's not, use a different query. But I can't get that to work for some odd reason.
Thank you for your help guys!