0

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>
  • Share the exact code you are trying. from your current code "$curYear & $curQuarter" is there from out of no where. – Sabbir Hasan May 7 '18 at 19:13
  • code <!-- get quarter --> <?php $curMonth = date("m", time()); $curQuarter = ceil($curMonth/3); $curQuarter = $curQuarter-1; ?> <!-- get year --> <?php $curYear = date('Y/m/d', strtotime('-14 day')); $curYear = date('Y', strtotime($curYear)); ?>code – Marek Ojciec Dyrektor Malinows May 8 '18 at 7:02
1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.