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