Jump to content

Recommended Posts

So, I have a problem that I've been trying to figure out for a while now on my own, but it's stressing me out because I need to get this project done soon. I'm working with dynamic checkboxes and SQL updates... here's the scenario:

 

I'm trying to allow certain users to access certain parts of the application. I have a table for users and a table for permissions. I select all users from the table and join the permissions table onto that (ON `id`), then using PHP, list checkboxes that are either checked or not.

 

The problem: I want to update that list by either inserting, updating, or deleting rows from the permissions table. For instance: User 1 and 2 currently have permissions, but user 3 does not. I want to update the list to allow user 1 and 3, but disallow user 2. HELP!

Link to comment
https://forums.phpfreaks.com/topic/169636-checkboxes/
Share on other sites

Users

id,

username

 

Permissions

id,

uid (User ID),

permid (Permission ID),

permval (Permission Value)

 

Query: SELECT * FROM users LEFT JOIN permissions ON users.id=permissions.uid

 

I really don't have any code that you can read right now, because I've chopped the crap out of it trying to get it working and it's a mess, but it basically goes... if permissions.id is null, then don't check the box, otherwise, do check the box. The problem isn't displaying that part, but how to handle the data after the form has been submitted. If I uncheck a box then it doesn't post to the next page and therefore can't be accounted for... so do I have to set all my permissions.permval to null, then go back and update them with the checked boxes from the form?

Link to comment
https://forums.phpfreaks.com/topic/169636-checkboxes/#findComment-894950
Share on other sites

Well, I figured out some code that "works" but I have a sneaking suspicion that there's a better way to do it...

<?php

$c = mysql_connect("localhost","username","password") or die(mysql_error());
$d = mysql_select_db("database");

if($_GET['submit'])
{

$q = mysql_query("DELETE FROM permissions") or die(mysql_error());

for($var=0;$var<count($_GET['user']);$var++)
{

  $ins .= "(NULL,'".$_GET['user'][$var]."','3','a'),";

}

$newins = rtrim($ins,",");

$q2 = mysql_query("INSERT INTO permissions (id,uid,optid,optval) VALUES $newins");

}


?>

<html>
<head>
<title>Form</title>
</head>
<body>

<form action="" method="get">
<?php

$c = mysql_connect("localhost","username","password") or die(mysql_error());
$d = mysql_select_db("database");

$q = mysql_query("SELECT * FROM users LEFT JOIN permissions ON users.id=test.uid") or die(mysql_error());

for($var=0;$var<mysql_num_rows($q);$var++)
{

$row = mysql_fetch_array($q);

if(!is_null($row[8]))
{

  $check = "checked=\"checked\"";

}

$list .= "<input type=\"checkbox\" name=\"user[]\" value=\"".$row[0]."\" $check>".$row[4]."<br>\n";

unset($check);

}

?>
<input type="submit" name="submit">
</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/169636-checkboxes/#findComment-895015
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.