0

I'm working on a project where user can fill out his/her profile. So while storing user's social media links, I've a json file which has all the refrences like name, favicon, url. So I can foreach it using json_decode and display with a text field. But how do I store all the links entered in the database as json format and retrieve them with the full URL.

This is json file

{
  "facebook": {
    "icon": "fab fa-facebook",
    "title": "Facebook",
    "url": "https://facebook.com/%s"
  },
  "instagram": {
    "icon": "fab fa-instagram",
    "title": "Instagram",
    "url": "https://instagram.com/%s"
  }
}

This is how I'm displaying it

 <?php foreach($available_buttons as $key => $button):  ?>
       <div class="col-md-4 mb-3">
            <div class="form-group">
                 <label ><i class="<?= $button->icon ?> mr-2"></i> <?= $button->title ?></label>
                 <input type="text" name="buttons[<?= $key ?>]" class="form-control" />
            </div>
       </div
 <?php endforeach; ?>

I'm processing it by using json_encode annd submitting it to db

$buttons = json_encode(clean_array($_POST['buttons']));

Passing the data in clean array function

function clean_array(Array $data) {
    foreach($data as $key => $value) {
        $data[$key] = filter_var($data, FILTER_SANITIZE_STRING);
    }

    return $data;
}


But when I try to submit i get false in database like "{'instagram': 'false', 'instagram': 'false'}"

How do I do it?

5
  • post name is not correct "buttons[<?= $key ?>]" . You are passing the name with dynamic value, but you are saving the result with not matching that name "$_POST['buttons']" Commented Sep 13, 2019 at 17:27
  • Instead of using echo for $key, you can simply wrap it with an equal sign to echo it <?= $key ?>, not an issue Commented Sep 13, 2019 at 17:30
  • I'm not telling about the echo function. telling about dynamic values in name Commented Sep 13, 2019 at 17:33
  • Question edited Commented Sep 13, 2019 at 17:36
  • please check your $_POST['buttons'] values after submit the form, and show us Commented Sep 13, 2019 at 17:42

2 Answers 2

2

You can store data as JSON in database and then get it again from database and convert it with json_decode. MySQL support JSON data type. Take a look on this link.

https://dev.mysql.com/doc/refman/5.7/en/json.html

Sign up to request clarification or add additional context in comments.

Comments

1

filter_var() can return FALSE when failed, check documentation: https://www.php.net/manual/en/function.filter-var.php

As first argument you should pass scalar or string, but you pass array $data (array is not scalar), therefore filter_var fails and returns false. Maybe you want to pass $value?

E.g. example of how your function should look like

function clean_array(Array $data) {
    foreach($data as $key => $value) {
        $data[$key] = filter_var($value, FILTER_SANITIZE_STRING);
    }

    return $data;
}

Comments

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.