Jump to content

Recommended Posts

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?!

 

Link to comment
https://forums.phpfreaks.com/topic/195706-many-check-boxes/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/195706-many-check-boxes/#findComment-1028168
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/195706-many-check-boxes/#findComment-1028174
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.