deagle25 Posted November 26, 2012 Share Posted November 26, 2012 Hi! I have followed this tutorial http://www.phpfreaks...-and-a-database and it is working fine but I would like to add four more columns with checkboxes to the right of the "Admin Privileges" column. You should be able to tick the checkboxes independantly of each other. I don't know how to adapt the php code sections to allow this. I hope someone can help. Username Admin Privileges ColA ColB ColC ColD Stewie [ ] [ ] [ ] [ ] [ ] Peter [ ] [ ] [ ] [ ] [ ] Brian [ ] [ ] [ ] [ ] [ ] Meg [ ] [ ] [ ] [ ] [ ] Lois .....and so on Chris Greased Up Deaf Guy Quagmire SubmitButton ****************** code *********************** <?php include("connect.php"); $updated = FALSE; if(count($_POST) > 0){ $admin = $_POST['admin']; array_map('intval',$admin); $admin = implode(',',$admin); mysql_query("UPDATE tutorial_users SET admin=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE tutorial_users SET admin=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR...l1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>phpfreaks checkbox tutorial</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <?php if($updated===TRUE){ echo '<div>Privileges Updated!</div>'; } ?> <table> <tr> <th>Username</th> <th>Admin Privileges</th> </tr> <?php $sql = "SELECT id,username,admin FROM tutorial_users ORDER by id ASC"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while(list($id,$username,$admin)=mysql_fetch_row($result)){ $checked = ($admin==1) ? 'checked="checked"' : ''; echo '<tr><td>'.$username.'</td><td><input type="checkbox" name="admin[]" value="'.$id.'" '.$checked.'/></td></tr>'."\n"; } ?> <tr><td colspan="2"><input type="submit" name="submit" value="Update Privileges" /></td></tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/271189-working-with-checkboxes-and-a-database/ Share on other sites More sharing options...
cyberRobot Posted November 26, 2012 Share Posted November 26, 2012 Are you just wondering how to add more checkboxes to the form? You should be able to add them to the while loop: while(list($id,$username,$admin)=mysql_fetch_row($result)){ $checked = ($admin==1) ? 'checked="checked"' : ''; echo '<tr><td>'.$username.'</td><td><input type="checkbox" name="admin[]" value="'.$id.'" '.$checked.'/></td>'; //<-- ADD CODE FOR THE CHECKBOXES HERE echo '</tr>'."\n"; } As a side note, I would recommend that you avoid using PHP_SELF as the <form> tag's action for security reasons. More information can be found here: http://seancoates.com/blogs/xss-woes Link to comment https://forums.phpfreaks.com/topic/271189-working-with-checkboxes-and-a-database/#findComment-1395173 Share on other sites More sharing options...
deagle25 Posted November 26, 2012 Author Share Posted November 26, 2012 I need help updating both the php code sections below with the additional columns ColA, ColB, ColC, ColD. If I start with the first php code below - should I add a copy of the code below for each new column and replace 'admin' with ColA and so on?: <?php include("connect.php"); $updated = FALSE; if(count($_POST) > 0){ $admin = $_POST['admin']; array_map('intval',$admin); $admin = implode(',',$admin); mysql_query("UPDATE tutorial_users SET admin=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE tutorial_users SET admin=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; } ?> ************second section I need help with************ The same question as before - do I copy the code and replace 'admin' with ColA' and so on? <?php $sql = "SELECT id,username,admin FROM tutorial_users ORDER by id ASC"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while(list($id,$username,$admin)=mysql_fetch_row($result)){ $checked = ($admin==1) ? 'checked="checked"' : ''; echo '<tr><td>'.$username.'</td><td><input type="checkbox" name="admin[]" value="'.$id.'" '.$checked.'/></td></tr>'."\n"; } ?> Thaks Link to comment https://forums.phpfreaks.com/topic/271189-working-with-checkboxes-and-a-database/#findComment-1395189 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.