V
V
Vladimir2018-10-02 19:58:16
PHP
Vladimir, 2018-10-02 19:58:16

Where is the error in reading using the POST method?

I can’t understand where I’m wrong ...
There is a form (Bootstrap 4) that includes a request and update of data (there are, in general, 5 fields + a button, I will show one field, the rest are similar):

<form role="form" name="GlobalsForm" action="" method="post">
<?php global_data_read (); ?> <!-- Читаем данные из БД -->
   <div class="form-group row h-20">
      <label for="site-name-input" class="col-2 col-form-label my-auto">Заголовок сайта</label>
      <div class="col-5 my-auto">
         <input class="form-control site-name" type="text" name="SiteName" value="<?php echo $GlobalData['SiteName']; ?>" id="site-name-input">
      </div>
      <span id="site-name-input" class="text-muted my-auto">Заголовок сайта. Применяется в названии вкладки браузера</span>
   </div>
   <button type="submit" name="submit" id="globals-data-save" class="btn btn-success btn-lg pull-right ">Сохранить общие настройки</button>
   <?php global_data_update (); ?> <!-- Изменяем данные в БД -->
</form>

The function of reading data from the global_data_read database:
function global_data_read () {
        global $GlobalData;
        $GlobalData = array();
        $bd_link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if (mysqli_connect_errno()) {
                printf("Не удалось подключиться: %s\n", mysqli_connect_error());
                exit();
        }
        if (!$bd_link->set_charset("utf8")) {
                printf("Ошибка при загрузке набора символов utf8: %s\n", $bd_link->error);
                exit();
        }
        $globals_query = "SELECT * FROM site_data";
        $globals_result = $bd_link->query($globals_query);
        while($globals_row = $globals_result->fetch_array()) {
                $globals_rows[] = $globals_row;
                $GlobalData['SiteName'] = $globals_row['SiteName'];
                $GlobalData['CompanyName'] = $globals_row['CompanyName'];
                $GlobalData['Theme'] = $globals_row['Theme'];
                $GlobalData['Phone'] = $globals_row['Phone'];
                $GlobalData['Email'] = $globals_row['Email'];
        }
        return $GlobalData;
}

Here everything works properly: the data is read and entered into the form fields.
The function global_data_update function for updating the data, working on a button click in the form:
function global_data_update () {
        $bd_link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if (mysqli_connect_errno()) {
                printf("Не удалось подключиться: %s\n", mysqli_connect_error());
                exit();
        }
        if (!$bd_link->set_charset("utf8")) {
                printf("Ошибка при загрузке набора символов utf8: %s\n", $bd_link->error);
                exit();
        }
        if (form_submitted()) {
                $GlobalData['SiteName'] = $_POST['SiteName'];
                echo "SiteName = " . $GlobalData['SiteName'] . "<br />";
                $GlobalData['CompanyName'] = $_POST['CompanyName'];
                echo "CompanyName = " . $GlobalData['CompanyName'] . "<br />";
                $GlobalData['Theme'] = $_POST['Theme'];
                echo "Theme = " . $GlobalData['Theme'] . "<br />";
                $GlobalData['Phone'] = $_POST['Phone'];
                echo "Phone = " . $GlobalData['Phone'] . "<br />";
                $GlobalData['Email'] = $_POST['Email'];
                echo "Email = " . $GlobalData['Email'];
                $globals_query = "UPDATE site_data SET
                        SiteName = '" . $GlobalData['SiteName'] . "',
                        CompanyName = '" . $GlobalData['CompanyName'] . "',
                        Theme = '" . $GlobalData['Theme'] . "',
                        Phone = '" . $GlobalData['Phone'] . "',
                        Email = '" . $GlobalData['Email'] . "'";
                $globals_result = $bd_link->query($globals_query);
        }
        return $GlobalData;
}

There is a form_submitted function here that just checks isset and $_POST.
Please note: in global_data_update I check and display the values ​​of the GlobalData array. This is where the glitch appears - the code does not recognize the Theme database field, although it successfully sees it when requested by the global_data_read function.
An error appears in the control output:
Notice: Undefined index: Theme in C:\Users\user\Desktop\XAMPP\htdocs\constreq\_db-operate\db-op.php on line 42
Theme =
Naturally, this field remains in the database empty, while the remaining fields are fasted, displayed and changed in the database correctly.
I'm already tired of checking the syntax, maybe my eyes are just blurry?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kulay, 2018-10-02
@Kulay

Try this: $GlobalData['Theme'] = isset($_POST['Theme']) ? $_POST['Theme'] : '';
And the error says that you are referring to a non-existent index, that is, the 'Theme' index does not exist in the $GlobalData array
PS: maybe there is some letter from Cyrillic instead of Latin, what the hell is not joking

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question