herghost Posted March 18, 2010 Share Posted March 18, 2010 I have a form with over 100 check boxes that needs inputting into a database, I am currently basically doing something like this: <?php session_start(); include('../../config/connect.php'); $name = (isset($_POST['name'])) ? trim($_POST['name']) : ''; $changemx = (isset($_POST['changemx'])) ? trim($_POST['changemx']) : ''; $traceaddy = (isset($_POST['traceaddy'])) ? trim($_POST['traceaddy']) : ''; $addoncgi = (isset($_POST['addoncgi'])) ? trim($_POST['addoncgi']) : ''; $addondomains = (isset($_POST['addondomains'])) ? trim($_POST['addondomains']) : ''; $zoneedit = (isset($_POST['zoneedit'])) ? trim($_POST['zoneedit']) : ''; $advguest = (isset($_POST['advguest'])) ? trim($_POST['advguest']) : ''; $agora = (isset($_POST['agora'])) ? trim($_POST['agora']) : ''; $analog = (isset($_POST['analog'])) ? trim($_POST['analog']) : ''; $handlers = (isset($_POST['handlers'])) ? trim($_POST['handlers']) : ''; $autoresponders = (isset($_POST['autoresponders'])) ? trim($_POST['autoresponders']) : ''; $awstats = (isset($_POST['awstats'])) ? trim($_POST['awstats']) : ''; $backup = (isset($_POST['backup'])) ? trim($_POST['backup']) : ''; $backupwizard = (isset($_POST['backupwizard'])) ? trim($_POST['backupwizard']) : ''; $bandwidth = (isset($_POST['bandwidth'])) ? trim($_POST['bandwidth']) : ''; $boxtrapper = (isset($_POST['boxtrapper'])) ? trim($_POST['boxtrapper']) : ''; $cgi = (isset($_POST['cgi'])) ? trim($_POST['cgi']) : ''; $csvimport = (isset($_POST['csvimport'])) ? trim($_POST['']) : 'csvimport'; $setlang = (isset($_POST['setlang'])) ? trim($_POST['setlang']) : ''; $style = (isset($_POST['style'])) ? trim($_POST['style']) : ''; $chat = (isset($_POST['chat'])) ? trim($_POST['chat']) : ''; $statselect = (isset($_POST['statselect'])) ? trim($_POST['statselect']) : ''; $counter = (isset($_POST['counter'])) ? trim($_POST['counter']) : ''; $cron = (isset($_POST['cron'])) ? trim($_POST['cron']) : ''; $errpgs = (isset($_POST['errpgs'])) ? trim($_POST['errpgs']) : ''; $defaultaddress = (isset($_POST['defaultaddress'])) ? trim($_POST['defaultaddress']) : ''; $dirselector = (isset($_POST['dirselector'])) ? trim($_POST['dirselector']) : ''; $diskusageviewer = (isset($_POST['diskusageviewer'])) ? trim($_POST['diskusageviewer']) : ''; $popaccts = (isset($_POST['popaccts'])) ? trim($_POST['popaccts']) : ''; $emailauth = (isset($_POST['emailauth'])) ? trim($_POST['emailauth']) : ''; $emaildomainfwd = (isset($_POST['emaildomainfwd'])) ? trim($_POST['emaildomainfwd']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $ = (isset($_POST[''])) ? trim($_POST['']) : ''; $query = "INSERT INTO product_features (name, changemx, traceaddy, addoncgi, addondomains, zoneedit, advguest, agora, analog, handlers, autoresponders, awstats, backup, backupwizard, bandwidth, boxtrapper, cgi, csvimport, setlang, style, chat, statselect, counter, cron, errpgs, defaultaddress, dirselector, diskusageviewer, popaccts, emailauth, emaildomainfwd VALUES ('$name', '$changemx', '$traceaddy', '$addoncgi', '$addondomains', 'zoneedit', 'advguest', '$agora', '$analog', '$handlers', '$autoresponders', '$awstats', '$backup', '$backupwizard', '$bandwidth', '$boxtrapper', '$cgi', '$csvimport', '$setlang', '$style', '$chat', '$statselect', '$counter', '$cron', '$errpgs', '$defaultaddress', 'dirselector', '$diskusageviewer', '$popaccts', '$emailauth', is there a quicker way?! Quote Link to comment https://forums.phpfreaks.com/topic/195706-many-check-boxes/ Share on other sites More sharing options...
premiso Posted March 18, 2010 Share Posted March 18, 2010 There is. $checkBoxes = array("name", "changemx". "traceaddy"); // etc.... foreach ($checkBoxes as $key) { $data[$key] = isset($_POST[$key])?trim($_POST[$key]) : ''; } $query = "INSERT INTO product_features (" . implode(", ", $checkBoxes) . ") VALUES (" . implode("', '", $data) . ")"; Something like that should make life a bit easier. Quote Link to comment https://forums.phpfreaks.com/topic/195706-many-check-boxes/#findComment-1028168 Share on other sites More sharing options...
herghost Posted March 18, 2010 Author Share Posted March 18, 2010 Thanks Could this be used with UPDATE as well? If so what would I need to change? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/195706-many-check-boxes/#findComment-1028171 Share on other sites More sharing options...
premiso Posted March 18, 2010 Share Posted March 18, 2010 Update is a bit more complicated, probably the easier way to do it would be: foreach ($checkBoxes as $key) { $val = isset($_POST[$key])?trim($_POST[$key]):''; $data[] = $key . " = '" . $val . "'"; } $query = "UPDATE table SET " . implode(", ", $data) . " WHERE blah = blah"; Quote Link to comment https://forums.phpfreaks.com/topic/195706-many-check-boxes/#findComment-1028174 Share on other sites More sharing options...
herghost Posted March 18, 2010 Author Share Posted March 18, 2010 Thanks again, Maybe ill just have an delete and add new ;p Quote Link to comment https://forums.phpfreaks.com/topic/195706-many-check-boxes/#findComment-1028190 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.