Dealing with $_POST and checkboxes and MySQLI

Ask about a PHP problem here.
Post Reply
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Dealing with $_POST and checkboxes and MySQLI

Post by killfrog47 »

Hello all =) So I have been doing great in problem solving the new project I have taken on. But I have run in to something I just cant seem to figure out. Here is what I am trying to accomplish:

I am making a permissions builder. IT will allow for a user to create a permissions file depending on the plugins he checks, and permission plugin he/she picks. Here is a basic drawing of what Im doin:
http://i.imgur.com/5ibEnn5.jpg?1

Now here is my code that I am running on the form.php and the test.php (the handler for the selections)
form.php:
<?php
include("config.php");
$mysqli = new mysqli($host, $db_uname, $db_pass, $db);
$query = "SELECT * FROM `plugins` WHERE 1";

if ($result = $mysqli->query($query)) {
	
	echo '<form action="test.php" method="post">
			<input name="gname" placeholder="Group Name..."/>
				<table width="200">
  					';
	
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
		
echo '<tr><td>
		<label>
      		<input type="checkbox" value="'.$row["plugin"].'" name="checkbox[]">
     		'.$row["plugin"].'
		</label>
		</td></tr>';

	}
	echo '
			</table>
			<select name="permplugin">
			<option>Select One...</option>';

$query2 = "SELECT * FROM `permission_types` WHERE 1";

if ($result2 = $mysqli->query($query2)) {
echo '<h3>Select Permission format below</h3><hr />';
while ($row2 = $result2->fetch_assoc()) {
echo '
<option value="'.$row2["plugin_name"].'">'.$row2["plugin_name"].'</option>';
}
echo '
</select>
<br />
<input name="" type="reset"><input name="" type="submit">
</form>';
}
}
?>
test.php:
<?php
if(!empty($_POST['checkbox']) || !empty($_POST['select']) || !empty($_POST['gname'])) {
	
	echo '<h1>'.$_POST['gname'].'</h1>';
	
	$check1 = $_POST['checkbox'];
    foreach($check1 as $check) {
		
            echo $check.'<br/>';
			
    }
	echo '<h3>Selected Permission format below</h3><hr />';
	echo $_POST['permplugin'];
	
} else {
	
	echo "please select atleast one plugin.";
}
?>
Now here is the problem. I can handle the $_POST['permplugin'] and the $_POST['gman']. But I dont know how to enter the $check from the foreach loop. I want to enter it in to a query so I can use it to get the permission nodes that go with the checked plugins. Though I keep running in to the same situation, I dont know how to handle a foreach loop and querys lol. Any help please?
Here are some shots of what I got so far:
http://i.imgur.com/2nVLdwv.png?1
http://i.imgur.com/Dml5sm9.png?1
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Dealing with $_POST and checkboxes and MySQLI

Post by killfrog47 »

I think I solved my own problem here. If i made a function say:
function checkboxes($check){
//connect to db
$query = //run a query using $check in it
echo //the results of the query
}
I just run the function in the foreach loop and it shuold be all good right?
Post Reply