/    Sign up×
Community /Pin to ProfileBookmark

numerous INSERT into DB If Statements.

Hi,
This is the most simple thing I want to do, but my brain seems to have fallen asleep!

What I want to do is when i add a user to a DB, i also want to set in another table the ‘permissions’ each user has by use of check boxes. If it is checked, then add it, if not, don’t.

What happens is that it inserts the 1st checked value, and then ignores the rest. So if i only selected one at a time, it’d add all i want, but if i check 2 or more, it only inserts the one the appears 1st.

the code i’ve done is:

[code=php]
$userid = htmlentities($_POST[‘userid’]);
$tooladd = htmlentities($_POST[‘tooladd’]);
$tooldelete = htmlentities($_POST[‘tooldelete’]);
$toolmodify = htmlentities($_POST[‘toolmodify’]);
$toolview = htmlentities($_POST[‘toolview’]);

$tooladd2 = ‘INSERT INTO dbtools.tblpermissions (userid, permiss) VALUES (“‘.$userid.'”, “tooladd”);’;
$tooldelete2 = ‘INSERT INTO dbtools.tblpermissions (userid, permiss) VALUES (“‘.$userid.'”, “tooldelete”);’;
$toolmodify2 = ‘INSERT INTO dbtools.tblpermissions (userid, permiss) VALUES (“‘.$userid.'”, “toolmodify”);’;
$toolview2 = ‘INSERT INTO dbtools.tblpermissions (userid, permiss) VALUES (“‘.$userid.'”, “toolview”);’;

if ($tooladd == “on”) {
$tooladd3 = mysql_query($tooladd2);
}
if ($tooldelete == “on”) {
$tooldelete3 = mysql_query($tooldelete2);
}
if ($toolmodify == “on”) {
$toolmodify3 = mysql_query($toolmodify2);
}
if ($toolview == “on”) {
$toolview3 = mysql_query($toolview2);
}
[/code]

I’m sure all it needs is a loop or something similar but my head ain’t workin!

Thanks in advance,
Rich

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 26.2008 — [code=php]
$userid = mysql_real_escape_string($_POST['userid']);
$values = array();
foreach(array('tooladd', 'tooldelete', 'tollmodify', 'toolview') as $field)
{
if($_POST[$field] == 'on')
{
$values[] = "('$userid', '$field')";
}
}
if(count($values))
{
$query = 'INSERT INTO dbtools.tblpermissions (userid, permiss) VALUES ' . implode(", ", $values);
$result = mysql_query($query);
}
[/code]
×

Success!

Help @slugmaster100 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...