Inserting Data from “Yes” / “No” checkboxes into database table in WordPress











up vote
0
down vote

favorite












So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.



I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.



Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)



HTML FORM



<form action="">
<div class="questions-main">
<div id="question-1" class="question">
<h4>Did you ever lose time from work or school due to gambling?</h4>
<input type="checkbox" id="q-one-yes" name="q-one-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-one-no" name="q-one-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-2" class="question">
<h4>Has gambling ever made your home life unhappy?</h4>
<input type="checkbox" id="q-two-yes" name="q-two-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-two-no" name="q-two-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-3" class="question">
<h4>Did gambling affect your reputation?</h4>
<input type="checkbox" id="q-three-yes" name="q-three-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-three-no" name="q-three-no" value="no" class="messageCheckbox">No</input>
</div>
</div>
</form>


Table Creation



    <?php 
global $aw_db_version;
$aw_db_version = "1.0";

function database_function() {
global $wpdb;
global $aw_db_version;

$tablename = $wpdb->prefix . "form_submission";

$charset_collate = $wpdb->get_charset_collate();

$sql="CREATE TABLE $tablename (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time_stamp datetime NULL,
question_one int(1) NULL,
question_two int(1) NULL,
question_three int(1) NULL,
PRIMARY KEY (id)
) $charset_collate;";

require_once( ABSPATH . "/wp-admin/includes/upgrade.php");
dbdelta($sql);

add_option( 'aw_db_version', $aw_db_version);

}


INSERT FUNCTION



<?php 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

if(isset($_POST)){
global $wpdb, $table_prefix;
$table_name = $wpdb->prefix . 'form_submission';
$timestamp = current_time( 'mysql' );
$q_1 = isset($_POST['q-one-yes']) ? 1 : 0;
$q_2 = isset($_POST['q-two-yes']) ? 1 : 0;
$q_3 = isset($_POST['q-three-yes']) ? 1 : 0;
var_dump($q_1, $q_20);
$wpdb->insert( $table_name, array(
'time_stamp' => $timestamp,
'question_one' => $q_1,
'question_two' => $q_2,
'question_three' => $q_3,
) );
echo "Working";
}else{
echo "Something's Gone Wrong Mate!";
};









share|improve this question


















  • 1




    Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
    – muka.gergely
    yesterday












  • I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
    – ajwerth
    yesterday

















up vote
0
down vote

favorite












So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.



I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.



Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)



HTML FORM



<form action="">
<div class="questions-main">
<div id="question-1" class="question">
<h4>Did you ever lose time from work or school due to gambling?</h4>
<input type="checkbox" id="q-one-yes" name="q-one-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-one-no" name="q-one-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-2" class="question">
<h4>Has gambling ever made your home life unhappy?</h4>
<input type="checkbox" id="q-two-yes" name="q-two-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-two-no" name="q-two-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-3" class="question">
<h4>Did gambling affect your reputation?</h4>
<input type="checkbox" id="q-three-yes" name="q-three-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-three-no" name="q-three-no" value="no" class="messageCheckbox">No</input>
</div>
</div>
</form>


Table Creation



    <?php 
global $aw_db_version;
$aw_db_version = "1.0";

function database_function() {
global $wpdb;
global $aw_db_version;

$tablename = $wpdb->prefix . "form_submission";

$charset_collate = $wpdb->get_charset_collate();

$sql="CREATE TABLE $tablename (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time_stamp datetime NULL,
question_one int(1) NULL,
question_two int(1) NULL,
question_three int(1) NULL,
PRIMARY KEY (id)
) $charset_collate;";

require_once( ABSPATH . "/wp-admin/includes/upgrade.php");
dbdelta($sql);

add_option( 'aw_db_version', $aw_db_version);

}


INSERT FUNCTION



<?php 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

if(isset($_POST)){
global $wpdb, $table_prefix;
$table_name = $wpdb->prefix . 'form_submission';
$timestamp = current_time( 'mysql' );
$q_1 = isset($_POST['q-one-yes']) ? 1 : 0;
$q_2 = isset($_POST['q-two-yes']) ? 1 : 0;
$q_3 = isset($_POST['q-three-yes']) ? 1 : 0;
var_dump($q_1, $q_20);
$wpdb->insert( $table_name, array(
'time_stamp' => $timestamp,
'question_one' => $q_1,
'question_two' => $q_2,
'question_three' => $q_3,
) );
echo "Working";
}else{
echo "Something's Gone Wrong Mate!";
};









share|improve this question


















  • 1




    Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
    – muka.gergely
    yesterday












  • I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
    – ajwerth
    yesterday















up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.



I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.



Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)



HTML FORM



<form action="">
<div class="questions-main">
<div id="question-1" class="question">
<h4>Did you ever lose time from work or school due to gambling?</h4>
<input type="checkbox" id="q-one-yes" name="q-one-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-one-no" name="q-one-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-2" class="question">
<h4>Has gambling ever made your home life unhappy?</h4>
<input type="checkbox" id="q-two-yes" name="q-two-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-two-no" name="q-two-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-3" class="question">
<h4>Did gambling affect your reputation?</h4>
<input type="checkbox" id="q-three-yes" name="q-three-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-three-no" name="q-three-no" value="no" class="messageCheckbox">No</input>
</div>
</div>
</form>


Table Creation



    <?php 
global $aw_db_version;
$aw_db_version = "1.0";

function database_function() {
global $wpdb;
global $aw_db_version;

$tablename = $wpdb->prefix . "form_submission";

$charset_collate = $wpdb->get_charset_collate();

$sql="CREATE TABLE $tablename (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time_stamp datetime NULL,
question_one int(1) NULL,
question_two int(1) NULL,
question_three int(1) NULL,
PRIMARY KEY (id)
) $charset_collate;";

require_once( ABSPATH . "/wp-admin/includes/upgrade.php");
dbdelta($sql);

add_option( 'aw_db_version', $aw_db_version);

}


INSERT FUNCTION



<?php 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

if(isset($_POST)){
global $wpdb, $table_prefix;
$table_name = $wpdb->prefix . 'form_submission';
$timestamp = current_time( 'mysql' );
$q_1 = isset($_POST['q-one-yes']) ? 1 : 0;
$q_2 = isset($_POST['q-two-yes']) ? 1 : 0;
$q_3 = isset($_POST['q-three-yes']) ? 1 : 0;
var_dump($q_1, $q_20);
$wpdb->insert( $table_name, array(
'time_stamp' => $timestamp,
'question_one' => $q_1,
'question_two' => $q_2,
'question_three' => $q_3,
) );
echo "Working";
}else{
echo "Something's Gone Wrong Mate!";
};









share|improve this question













So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.



I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.



Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)



HTML FORM



<form action="">
<div class="questions-main">
<div id="question-1" class="question">
<h4>Did you ever lose time from work or school due to gambling?</h4>
<input type="checkbox" id="q-one-yes" name="q-one-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-one-no" name="q-one-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-2" class="question">
<h4>Has gambling ever made your home life unhappy?</h4>
<input type="checkbox" id="q-two-yes" name="q-two-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-two-no" name="q-two-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-3" class="question">
<h4>Did gambling affect your reputation?</h4>
<input type="checkbox" id="q-three-yes" name="q-three-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-three-no" name="q-three-no" value="no" class="messageCheckbox">No</input>
</div>
</div>
</form>


Table Creation



    <?php 
global $aw_db_version;
$aw_db_version = "1.0";

function database_function() {
global $wpdb;
global $aw_db_version;

$tablename = $wpdb->prefix . "form_submission";

$charset_collate = $wpdb->get_charset_collate();

$sql="CREATE TABLE $tablename (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time_stamp datetime NULL,
question_one int(1) NULL,
question_two int(1) NULL,
question_three int(1) NULL,
PRIMARY KEY (id)
) $charset_collate;";

require_once( ABSPATH . "/wp-admin/includes/upgrade.php");
dbdelta($sql);

add_option( 'aw_db_version', $aw_db_version);

}


INSERT FUNCTION



<?php 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

if(isset($_POST)){
global $wpdb, $table_prefix;
$table_name = $wpdb->prefix . 'form_submission';
$timestamp = current_time( 'mysql' );
$q_1 = isset($_POST['q-one-yes']) ? 1 : 0;
$q_2 = isset($_POST['q-two-yes']) ? 1 : 0;
$q_3 = isset($_POST['q-three-yes']) ? 1 : 0;
var_dump($q_1, $q_20);
$wpdb->insert( $table_name, array(
'time_stamp' => $timestamp,
'question_one' => $q_1,
'question_two' => $q_2,
'question_three' => $q_3,
) );
echo "Working";
}else{
echo "Something's Gone Wrong Mate!";
};






php wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









ajwerth

437




437








  • 1




    Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
    – muka.gergely
    yesterday












  • I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
    – ajwerth
    yesterday
















  • 1




    Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
    – muka.gergely
    yesterday












  • I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
    – ajwerth
    yesterday










1




1




Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday






Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday














I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday






I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416973%2finserting-data-from-yes-no-checkboxes-into-database-table-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416973%2finserting-data-from-yes-no-checkboxes-into-database-table-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)