oolongdavies Posted May 21, 2007 Share Posted May 21, 2007 The following code selects checkbox values and posts them back to the page in an array. I can get the two echo statements at the bottom of the script to display the correct results, and I can even add the checkbox values to the database. However, the table 'tbl_product_risks' contains two fields, fld_risk and fld_username. The username is stored in $_SESSION['username'] and I would like to add this value to the database (fld_username) for each checkbox value (fld_risk) that is inserted - so the records would be inserted 'checkboxvalue', 'username'. I am a php novice and I would appreciate any help (I have been all day on this and I seem to be going round in circles!) <?include_once('dbconn.php');?> <form name = "addmsds3" action="index3.php" method="post"> <?php $sql = "SELECT * FROM tbl_risk_phrases"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { echo '<input type="checkbox" name="selection[]" value="' . $row['fld_risk_phrase_id'] . '">'; echo '' . $row['fld_risk_phrase_id'] . ''; echo '<br>'; } ?> <input type="submit" value="submit" name="submit"> </form> <br> <?php if(!isset($_POST)) exit(); $selections = (isset($_POST['selection']))?$_POST['selection']:NULL; $count = count($selections); $query = 'INSERT INTO tbl_product_risks (fld_risk) VALUES ('; $add = "'".implode("','",$selections)."'"; $query .= $add.')'; echo $query; echo '<br>'; echo $count ?> Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/ Share on other sites More sharing options...
corbin Posted May 21, 2007 Share Posted May 21, 2007 You could just do a foreach loop. Something basically like foreach($_POST['selection'] as $k => $v) { //check the value //if the value is legit { insert into db } } Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258561 Share on other sites More sharing options...
oolongdavies Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks for that, but what about the username variable? I understand the concept but not the code! Any help would really be appreciated. Tia J Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258565 Share on other sites More sharing options...
Corona4456 Posted May 21, 2007 Share Posted May 21, 2007 foreach($_POST['selection'] as $k => $v) { $query = 'INSERT INTO tbl_product_risks (fld_username, fld_risk) VALUES ('.$_SESSION['username'].','.$_POST['selection'].')'; } Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258573 Share on other sites More sharing options...
oolongdavies Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks for that but I can't get it to work. At the moment, the echo $query statement just displays "INSERT INTO table (fld_username, fld_risk) VALUES ("jupiter","Array")" I'deally, here's what I'd like - if 4 boxes are checked, then I need to insert 4 records where each record has the $SESSION['username'] value and one of the checkbox values. Alternatively, I could live with one row that contains the username and an array of the checkbox values. However, I have no idea how to retrieve them... Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258593 Share on other sites More sharing options...
Corona4456 Posted May 21, 2007 Share Posted May 21, 2007 err... oops... sorry for that, here's the updated code: <?php foreach($_POST['selection'] as $k => $v) { $query = 'INSERT INTO tbl_product_risks (fld_username, fld_risk) VALUES ('.$_SESSION['username'].','.$v.')'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258601 Share on other sites More sharing options...
oolongdavies Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks for that, I'm getting closer... That does work but it only adds the value of the last checkbox selected. Ideally, I would like to add each checkbox value (and username) to a new row in the db - so if 5 checkboxes are ticked, 5 new records are inserted. I really appreciare your help. TIA J Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258622 Share on other sites More sharing options...
Corona4456 Posted May 21, 2007 Share Posted May 21, 2007 <?php foreach($_POST['selection'] as $k => $v) { $query = 'INSERT INTO tbl_product_risks (fld_username, fld_risk) VALUES ('.$_SESSION['username'].','.$v.')'; mysql_query($query); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258633 Share on other sites More sharing options...
oolongdavies Posted May 22, 2007 Author Share Posted May 22, 2007 Brilliant - You the man! Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258672 Share on other sites More sharing options...
Corona4456 Posted May 22, 2007 Share Posted May 22, 2007 I do what I can... heh... Quote Link to comment https://forums.phpfreaks.com/topic/52393-solved-something-simple/#findComment-258745 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.