0

I have problem with my code. I don't know why all my $_Post have null at the end.This is my code:

    function ow_display_user() {
    wp_enqueue_script('jquery-ui-datepicker');
    wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');

    global $wpdb;
    $results = $wpdb->get_results ("SELECT adres FROM wp_ow_adres;");
    ?>
<h2>Odczyt wodomierzy</h2>
<form method="post" action="">
    <table>
        <tbody>
            <tr>
                <th><label for="Adres">Adres zamieszkania</label></th>
                <td><select id="adres" name="adres">
                        <option value="">Wybierz swój adres</option>
                        <?php 
                        foreach ( $results as $result ) {
        echo '<option>'.$result->adres.'</option>';
    }
    ?>
                </select></td>
            </tr>
            <input type="submit" name="submit" value="Wybierz" />
        </tbody>
    </table>
</form>
<?php 
$adres = $_POST["adres"];
global $wpdb;
if (isset($_POST['submit'])){
        $spr_liczbe_wodo = $wpdb->get_var("select liczba_wodomierzy from wp_ow_adres where adres='$adres'");
        switch ($spr_liczbe_wodo) {
            case 1:
                ?>
<form name="adres_form" method="post" action="">
    <table>
        <tbody>
            <tr>
                <td>Adres zamieszkania:</td>
                <td><?php echo $adres;?></td>
            </tr>
            <tr>
                <td>Stan wodomierza:</td>
                <td><input type="text" name="stan_wodomierza" /></td>
            </tr>
            <tr>
                <td>Data odczytu:</td>
                <td><input type="text" name="data_odczytu" id="datepicker" /></td>
            </tr>
            <tr>
                <td>Kod lokalu:</td>
                <td><input type="text" name="kod_lokalu" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="Zapisz" /></td>
            </tr>
        </tbody>
    </table>
</form>
<?php 
break;
case 2:
    ?>
<form name="adres_form" method="post" action="">
    <table>
        <tbody>
            <tr>
                <td>Adres zamieszkania:</td>
                <td><?php echo $adres;?></td>
            </tr>
            <tr>
                <td>Woda ciepła:</td>
                <td><input type="text" name="woda_ciepla" /></td>
            </tr>
            <tr>
                <td>Woda zimna:</td>
                <td><input type="text" name="woda_zimna" /></td>
            </tr>
            <tr>
                <td>Data odczytu:</td>
                <td><input type="text" name="data_odczytu" id="datepicker" /></td>
            </tr>
            <tr>
                <td>Kod lokalu:</td>
                <td><input type="text" name="kod_lokalu" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="Zapisz" /></td>
            </tr>
        </tbody>
    </table>
</form>
<?php
break;
case 4:
    ?>
<form method="POST" action="">
    <table>
        <tbody>
            <tr>
                <td>Adres zamieszkania:</td>
                <td><?php echo $adres;?></td>
            </tr>
            <tr>
                <td>Woda ciepła (kuchnia):</td>
                <td><input type="text" name="woda_ciepla_kuchnia" /></td>
            </tr>
            <tr>
                <td>Woda ciepła (łazienka):</td>
                <td><input type="text" name="woda_ciepla_lazienka" /></td>
            </tr>
            <tr>
                <td>Woda zimna (kuchnia):</td>
                <td><input type="text" name="woda_zimna_kuchnia" /></td>
            </tr>
            <tr>
                <td>Woda zimna (łazienka):</td>
                <td><input type="text" name="woda_zimna_lazienka" /></td>
            </tr>
            <tr>
                <td>Data odczytu:</td>
                <td><input type="text" name="data_odczytu" id="datepicker" /></td>
            </tr>
            <tr>
                <td>Kod lokalu:</td>
                <td><input type="text" name="kod_lokalu" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="Zapisz" /></td>
            </tr>
        </tbody>
    </table>
</form>
<?php
if (isset($_POST['submit'])){
    global $wpdb;
    $kod_lokalu = $_POST['kod_lokalu'];
    $spr_kod_lokalu = $wpdb->get_var("select kod_lokalu from wp_ow_adres where adres='. $adres .'");
    if ($spr_kod_lokalu == $kod_lokalu) {
            $kod_lokalu = $_POST['data_odczytu'];
            $woda_ciepla_kuchnia = $_POST['woda_ciepla_kuchnia'];
            $woda_ciepla_lazienka = $_POST['woda_ciepla_lazienka'];
            $woda_zimna_kuchnia = $_POST['woda_zimna_kuchnia'];
            $woda_zimna_lazienka = $_POST['woda_zimna_lazienka'];
            $data_odczytu = '0000-00-00 00:00:00';

            $data = array(
                    'id' => '',
                    'adres' => $adres,
                    'data_odczytu' => $data_odczytu,
                    'woda_ciepla_kuchnia' => $woda_ciepla_kuchnia,
                    'woda_ciepla_lazienka' => $woda_ciepla_lazienka,
                    'woda_zimna_kuchnia' => $woda_zimna_kuchnia,
                    'woda_zimna_lazienka' => $woda_zimna_lazienka,
            );

            $wpdb->insert(`wp_ow_odczyty`, $data);
    }
}
break;
}

    }
}
9
  • The fact that $_POST['submit'] is set does not mean that the other $_POST keys are. Have you checked them all?
    – s_ha_dum
    Commented Dec 29, 2013 at 15:41
  • @s_ha_dum what do you mean ? and other variable to 'isset' ? Commented Dec 29, 2013 at 16:17
  • You check for $_POST['submit'] but you actually use $_POST['woda_ciepla_kuchnia'] and $_POST['kod']. That $_POST['submit'] is set does not guarantee that the other two are set, much less set correctly.
    – s_ha_dum
    Commented Dec 29, 2013 at 16:36
  • Okey I get it. But I input all that fields and I have null. If I change $woda_ciepla_kuchnia = $_POST['woda_ciepla_kuchnia']; to $woda_ciepla_kuchnia = 1; it's works. Commented Dec 29, 2013 at 16:41
  • But did you verify that the form is sending the fields correctly? Just var_dump($_POST); on the page and look.
    – s_ha_dum
    Commented Dec 29, 2013 at 16:42

1 Answer 1

0

Please show Your html form code.

Also i think you have an error in $wpdb->insert statment. Should be:

$wpdb->insert(wp_ow_odczyty, array($data));
3
  • If I input number or string into a variable I have insert into db. Commented Dec 29, 2013 at 13:52
  • This code is incorrect. It throws an "undefined constant Notice and also creates a multidimensional array which triggers a Warning and causes a database error and $wpdb->insert does not expect a nested array.
    – s_ha_dum
    Commented Dec 29, 2013 at 17:14
  • When I move code from case 4 (switch) above it's works. Why ? Commented Dec 29, 2013 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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