In my db MySQL, I have a table ranks
with many fields, one for each page I want limit access for the user in the menu application with PHP control:
<?php if ($row_ranks['padric'] == '1' ) {
echo ('<li><a href="padroncini_ric.php">Ricerca</a></li>');
}
else {
echo ('<li><a href="#" class="disabled">Ricerca</a></li>');
}
?>
In the admin panel, I have a page for seting the privileges to single page with relative checkbox.
The checkbox as a name equal at field name in table like this example:
<div>
<input type="checkbox" name="padric" value="1"
<?php if ($row_user_ranks['padric'] == 1) { echo "checked"; } ?>
>Ricerca
</div>
For set the privileges I make this PHP code:
mysql_select_db($database_geomo, $geomo);
$query_RSfields = sprintf("SHOW COLUMNS FROM ranks WHERE Field NOT IN ('ID', 'userID', 'gruppo')");
$RSfields = mysql_query($query_RSfields, $geomo) or die(mysql_error());
$row_RSfields = mysql_fetch_assoc($RSfields);
$totalRows_RSfields = mysql_num_rows($RSfields);
do {
$field = $row_RSfields['Field'] ;
if (!isset($_POST[$field])) { // set value = 0 for checkbox not checked
$_POST[$field] = '0' ;
}
$updateSQL = sprintf("UPDATE ranks SET $field='".$_POST[$field]."' WHERE userID='".$_POST['ID']."'"); // set privileges for user
mysql_select_db($database_geomo, $geomo);
$Result1 = mysql_query($updateSQL, $geomo) or die(mysql_error());
} while ($row_RSfields = mysql_fetch_assoc($RSfields));
I've made this code in this way because if the checkbox is not checked, it shouldn't return a value and should generate an error "unknown column" for the update SQL.
So, there is another more simply way for doing this job?
mysql_query
. It has been deprecated for a while now, and only dinosaurs, "24 hours" noobs, and w3schools dropouts still use it. – cHao Feb 14 '14 at 14:32