Question

I am trying to solve one problem I have. I created a form that allows users to submit data to special table in my database. I want them to input one field and I want to get the rest information fed from other variables in the file that I have define. Unfortunately it does not post to database. Can someone have a look if I have any errors in my code? Thx in advance

<?php
if (!empty($_POST)) {
    global $wpdb;
    $table = wattp2_as_score;
    $data = array(
        'score_as' => $_POST['yourname'],
        'year' => $_POST[$curYear],
        'quarter' => $_POST[$curQuarter],
    );
    $format = array(
        '%s',
        '%s',
        '%s',
    );
    $success=$wpdb->insert( $table, $data, $format );
    if($success){
        echo 'data has been save' ; 
    }
} else {
    ?>
    <form method="post">
        <input type="text" name="yourname">
        <input type="submit">
    </form>
Was it helpful?

Solution

Modify your code like this. It should work now. What you did wrong is both $curQuarter & $curYear is php variable not a part POST. you can directly use them.

<?php

$curMonth = date("m", time()); 
$curQuarter = ceil($curMonth/3);
$curQuarter = $curQuarter-1;

$curYear = date('Y/m/d', strtotime('-14 day')); 
$curYear = date('Y', strtotime($curYear)); 

if (!empty($_POST)) {
    global $wpdb;
    $table = wattp2_as_score;

    $data = array(
        'score_as' => $_POST['yourname'],
        'year' => $curYear,
        'quarter' => $curQuarter,
    );
    $format = array(
        '%s',
        '%s',
        '%s',
    );
    $success=$wpdb->insert( $table, $data, $format );
    if($success){
        echo 'data has been save' ; 
    }
} else {
    ?>
    <form method="post">
        <input type="text" name="yourname">
        <input type="submit">
    </form>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top