1

I am working on an admin panel for my website that will change website settings. Everything works the way it's supposed to, except for the number of PHP warnings I get when a submit some values. I get the error undefined index. I know you can use isset(), but in my case, that would be very messy. How would I use a value in a database as a default value if my value is not set?

My code:

<?php
    
 if(!empty($_POST)) {
    $_POST['cms_and_posting'] = (bool) $_POST['cms_and_posting'];
    $_POST['google_verify'] = (bool) $_POST['google_verify'];
 }

?>

I have heard of something called the "null-coalescing-operator" in PHP, but I am a bit confused on how I would use that in my code.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Heard of the "[null coalescing operator](https://stackoverflow.com/q/34571330/2943403)"? – mickmackusa Sep 28 '20 at 16:53
  • @mickmackusa I have not, please give me some more info. –  Sep 28 '20 at 16:54
  • @mickmackusa Could you perhaps give me an example based on my code above? –  Sep 28 '20 at 16:58
  • I don't find your question to be clear. I am on my phone. (And since it is 3am, I'm going back to bed) I don't know which lines are the numbers you have mentioned. Please read about how to post a [mcve]. – mickmackusa Sep 28 '20 at 16:59
  • you could pre-populate your HTML form with default values. But if the field can be optional, you need to use isset(). That's just the way it is. – ADyson Sep 28 '20 at 17:04
  • @ADyson Is it possible to use the "null coalescing operator" as mickmackusa has stated in my case? –  Sep 28 '20 at 17:06
  • undefined index means either one value is not posted via form or request, check your method if both the values are being sent or not. you are missing one value thats why it is giving undefined index. post both values $_POST['cms_and_posting'] , $_POST['google_verify'] – Rohit Guleria Sep 28 '20 at 17:33

2 Answers2

2

You can use null coalescing operator.

 <?php
    
  if(!empty($_POST)) {
    $_POST['cms_and_posting'] = $_POST['cms_and_posting'] ?? $youdbvalue1;
    $_POST['google_verify'] =   $_POST['google_verify'] ?? $youdbvalue2;
  }

?>
Lucas Gervas
  • 369
  • 2
  • 5
  • Why cant you use (bool) in this case? –  Sep 28 '20 at 17:50
  • @lucas when a page can be resolved by citing a pre-existing page, please vote to close instead of answering. This way Stack Overflow doesn't need to store redundant pages. – mickmackusa Sep 28 '20 at 21:12
0

You have a series of settings from the database, so I figure you have something like

$options['cms_and_posting'] = 1;

You can then use foreach():

foreach (array_keys($options) as $key) {
    if (array_key_exists($key, $_POST)) {
        $options[$key] = $_POST[$key];
    }
}

Note that, here, you do not have any check on what the value is. Usually your configuration table would be something like

varname           varvalue          vartype    varregex    varcomment
cms_and_posting   1                 bool       ^[01]$      Option
google_verify     1                 bool       ^[01]$      Option
user_email        lserni@gmail.com  email      NULL        Admin Email
pagesize          50                int        ^\\d+$      Rows per page
...

You would still run a foreach cycle, using the filter functions to appropriately validate each entry:

switch($option['vartype']) {
    case 'bool':
        if (in_array($value, [ '1', 'Y', 'y', 'YES', 'ON', 1 ], true)) {
            $value = true;
        } else if (in_array($value, [ '0', 'N', 'n', 'NO', 'OFF', 0 ], true)) {
            $value = false;
        } else {
            throw new \Exception("{$value} is not a valid boolean");
        }
        break;
    case 'email':
        ...
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • please help me with this question. https://stackoverflow.com/questions/64081641/add-div-tag-inside-all-li-with-matching-class-name-using-php/64092865#64092865 – suryavansh Sep 28 '20 at 18:08