jbehen Posted December 10, 2006 Share Posted December 10, 2006 First time here, and I am at my wits end. I have googled until my fingers bled, so I am hoping I can find help here.I have a MySQL table called osi_menurights, consisting of:user_namegenindcgdcpvdcpmadminWith the exception of the username, the other fields are boolean flags, determining whether the user has rights to display that particular menu.The database is querying correctly, as the following code:[code]<?phprequire ('includes/db_vars.php');$user = $_POST['user_name'];echo "<h1>$user</h1>\n" . "<FORM ACTION=\"test.php\" METHOD=\"POST\">\n";$query = "SELECT gen, ind, cg, dcpv, dcpm, admin FROM osi_menurights WHERE user_name = '$user'";$result = mysql_query($query);$rowcount = 1;while ($row = mysql_fetch_assoc($result)) { foreach($row as $var => $val) { echo "$var: $val<br/>\n"; } ++$rowcount;} ?>[/code]results in the following:[quote]jbehengen: 1ind: 0cg: 1dcpv: 0dcpm: 0admin: 1[/quote]My goal is to display the results as checkboxes, with the "CHECKED" status being determined by the flag. My code for this is:[code]<?phprequire ('includes/db_vars.php');$user = $_POST['user_name'];echo "<h1>$user</h1>\n" . "<FORM ACTION=\"test.php\" METHOD=\"POST\">\n";$query = "SELECT gen, ind, cg, dcpv, dcpm, admin FROM osi_menurights WHERE user_name = '$user'";$result = mysql_query($query);$rowcount = 1;while ($row = mysql_fetch_assoc($result)) { foreach($row as $var => $val) { echo "<input TYPE=CHECKBOX NAME=\"$var\""; if ($val = '1') { echo " CHECKED"; } echo "/> $var<br/>\n"; } ++$rowcount;} ?>[/code]It displays the checkboxes, followed the table field name. But for some reason, every checkbox is checked.This is the HTML code generated (Don't know if I can build a form in a post, so displaying the HTML only):[code]<h1>jbehen</h1><FORM ACTION="test.php" METHOD="POST"><input TYPE=CHECKBOX NAME="gen" CHECKED/> gen<br/><input TYPE=CHECKBOX NAME="ind" CHECKED/> ind<br/><input TYPE=CHECKBOX NAME="cg" CHECKED/> cg<br/><input TYPE=CHECKBOX NAME="dcpv" CHECKED/> dcpv<br/><input TYPE=CHECKBOX NAME="dcpm" CHECKED/> dcpm<br/><input TYPE=CHECKBOX NAME="admin" CHECKED/> admin<br/>[/code]Any ideas? Quote Link to comment Share on other sites More sharing options...
Mr_Pancakes Posted December 10, 2006 Share Posted December 10, 2006 easy fix: change that single "=" character to "=="[code]if ($val == '1') { echo " CHECKED";}[/code]the single "=" character is setting $val to 1 and will always be true. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 10, 2006 Share Posted December 10, 2006 ^expanded:= is the assignment operator. == is the equality operator. But i'm sure you knew that, and it was just a typo. Quote Link to comment Share on other sites More sharing options...
jbehen Posted December 10, 2006 Author Share Posted December 10, 2006 Well, son of a ....[code]<h1>jbehen</h1><FORM ACTION="test.php" METHOD="POST"><input TYPE=CHECKBOX NAME="gen" CHECKED/> gen<br/><input TYPE=CHECKBOX NAME="ind"/> ind<br/><input TYPE=CHECKBOX NAME="cg" CHECKED/> cg<br/><input TYPE=CHECKBOX NAME="dcpv"/> dcpv<br/><input TYPE=CHECKBOX NAME="dcpm"/> dcpm<br/><input TYPE=CHECKBOX NAME="admin" CHECKED/> admin<br/>[/code]It worked! Much thanks!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.