0

I code an edit page for my blog, but not all data from database are displayed. Just 3 / 7 = title, point, publish. Option, seo, perex and content are not. Pic is irrelevant because there is no option to upload yet. Here is my code:

    if(is_post_request()) {
    $page = [];
    $page['id'] = $id;
    $page['title'] = $_POST['title'] ?? '';  
$page['pic'] = $_POST['pic'] ?? '';
    $page['perex'] = $_POST['perex'] ?? '';          
    $page['option'] = $_POST['option'] ?? '';
    $page['seo'] = $_POST['seo'] ?? '';
    $page['pointa'] = $_POST['pointa'] ?? '';
    $page['content'] = $_POST['content'] ?? '';
    $page['publish'] = $_POST['publish'] ?? '';
    
    $result = update_page($page);
    if($result === true) {
        redirect_to(url_for('/blog/public/clanoktemplate.php?id=' . $id));
    } else {
    $errors = $result;
    }
} else {
    $page = find_page_by_id($id);
} 
    
    <form action="<?php echo url_for('/blog/private/edit.php?id=' . h(u($id))); ?>" method="POST">
            <h2>Tu môžeš upraviť článok</h2>
                <div class="part">
                    <label for="title">Názov článku:</label><br>
                    <input type="text" id="title" name="title" value="<?php echo h($page['title']); ?>" required><br>
                </div>
                <div class="part">
                    <label for="option">Kategória:</label>
                    <select type="option" id="option" name="option" value="<?php echo h($page['option']); ?>" required>
                            <option value="pristup">Prístup</option>
                            <option value="sebaedukacia" selected>Sebaedukácia</option>
                            <option value="sebarodicovstvo">Sebarodičovstvo</option>
                            <option value="zasadneschopnosti">Zásadné schopnosti</option>
                            <option value="dusevnymajetok">Duševný majetok</option>
                            <option value="vzdelaniebuducnosti">Vzdelanie budúcnosti</option>
                        </select><br>
                </div>
                <div class="part">
                    <label for="seo" id="seo" name="seo">SEO:</label><br>
                    <input type="text" id="seo" name="seo" value="<?php echo h($page['seo']); ?>"><br>
                </div>
                <div class="part">
                    <label for="pointa" id="pointa" name="pointa">Hlavná myšlienka:</label><br>
                    <input type="text" id="pointa" name="pointa" value="<?php echo h($page['pointa']); ?>"><br>
                </div>
                <div class="part">
                    <label for="perex" id="perex" name="perex">Perex:</label><br>
                    <textarea id="perex" name="perex" rows="4" value="<?php echo h($page['perex']); ?>" required></textarea><br>
                </div>
                <button type="button" id="pic" name="pic"> Nahrať obrázok </button><br>
                <div class="part">
                    <label for="content" id="content" name="content">Celý článok:</label><br>
                    <textarea id="content" name="content" rows="20" value="<?php echo h($page['content']); ?>"required></textarea><br>
                </div>
                <div class="mini-part" style="padding-bottom: 15px; font-size: 20px; text-align: left; ">
                    <label for="visible" id="publish" name="publish">Viditeľný:</label>
                    <input type="hidden" id="publish" name="publish" value="0">
                        <input type="checkbox" id="publish" name="publish" value="1"<?php if($page['publish'] == "1") { echo " checked"; } ?>><br>
                </div>

Why is that? How can I fix it? Thank you for your help.

Par ex. output of var_dump($page); => array(10) { ["id"]=> string(3) "148" ["title"]=> string(7) "Testing" ["seo"]=> string(9) "seo words" ["pic"]=> string(0) "" ["perex"]=> string(38) "Testing is needed before going online." ["content"]=> string(637) "The whole comes here. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Everything except this editing page work just fine. " ["datum"]=> string(19) "2022-04-08 20:59:15" ["pointa"]=> string(25) "the main point comes here" ["kateg"]=> string(17) "zasadneschopnosti" ["publish"]=> string(1) "1" }

It looks perfectly fine.

6
  • what is find_page_by_id doing? I'd bet it's only returning title, point, and publish. And what is h() doing when echoing $page['seo']? Commented Apr 1, 2022 at 17:53
  • h() stands for for htmlspecialchars(). So you can ignore that part. Find_page_by_id should return all of tha data. function find_page_by_id($id) { global $db; $sql = "SELECT * FROM article WHERE id='" . db_escape($db, $id) . "'"; $result = mysqli_query($db, $sql); confirm_result_set($result); $page = mysqli_fetch_assoc($result); mysqli_free_result($result); return $page; } Commented Apr 1, 2022 at 18:17
  • what outputs when you do var_dump($page); die(); after $page = find_page_by_id($id);? Can you prove to use that article has those columns? Commented Apr 1, 2022 at 18:50
  • It correctly displays all of the data. The ones that are not displayed in my form, too. Commented Apr 2, 2022 at 8:27
  • Are you sure that $page is an array and has all of those keys? Per the example I put together at 3v4l.org/rE9pr it works just fine as long as $page is an associative array with the expected keys. Are you inspecting the HTML to confirm if the inputs' value parameter is empty, or are you just viewing the rendered page? Please edit your question to include an example of a full var_dump of the $pages variable. Commented Apr 3, 2022 at 18:05

1 Answer 1

0

You are using textarea incorrectly. Textareas do not use a value= attribute.

Should be

 <textarea> ...value of data here </textarea>

Fix your code like so:

 <textarea id="perex" name="perex" rows="4" required><?php echo h($page['perex']); ?></textarea><br>

You have a similar issue with option. Again the select tag does not have a value attribute. Each individual option has a value attribute and when the form is submitted, the value will be set to the value of the option that was selected.

For display of the option you need to write code that matches the value you are setting in your database to the value of the option, and for the option that matches the prior value, you need to add "selected".

Typically this is done with a ternary, so for each option you need something like this:

  <option value="pristup" <?php h($page['option']) === 'pristup' ? ' selected' : '' ?>>Prístup</option>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you gview, now the parts of perex and content are working. Just option left.
I updated the answer to explain your mistake with option. In general, you have to understand how html form elements work in order to make them work with php.
Yes, it is similar also to $page['publish']. I thought it would be using foreach or something like that (not individually). Thank you for your help.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.