-2

I am saving a popup modules settings and when is_popup is unchecked I receive the error below but it will still save the settings to the DB table, I don't get an error if checked.

ERROR when "is new target" is unchecked

enter image description here

Notice: Undefined index: is_popup in admin/model/design/popupbanner.php on line 48Notice: Undefined index: is_popup in admin/controller/extension/module/popupbanner.php on line 44Warning: Cannot modify header information - headers already sent by (output started at admin/controller/startup/error.php:34) in system/library/response.php on line 12

I've done some var_dumps and I can see is_popup is bringing back either Y or N so unsure why I'm getting undefined index.

MODEL - admin/controller/extension/module/popupbanner.php

43 $sql = "update `".DB_PREFIX."popupbanner_setting` set
44         `title` = '".$this->db->escape($all_post["title"])."',
45         `desc` = '".$this->db->escape($all_post["desc"])."',
46         `weblink` = '".$this->db->escape($all_post["weblink"])."',
47         `bannerfile` = '".$this->db->escape($all_post["bannerfile"])."',
48         `is_popup` = '".$this->db->escape($all_post["is_popup"] == "Y" ? "Y" : "N")."'
49           where `id` = '".$this->module_setting_id."'";
50 $rs = $this->db->query($sql);
51 }

CONTROLLER - admin/controller/extension/module/popupbanner.php

44 if($this_request_post["is_popup"] == ""){
45   $this_request_post["is_popup"] = "N";
46  }

VIEW -

<input type="checkbox" name="is_popup" value="Y" <?=($is_popup == "Y" ? "checked" : "")?> />

any help would be appreciated.

FishLiam
  • 31
  • 7
  • **Warning:** You might be open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](http://php.net/manual/en/pdo.prepared-statements.php) or by [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). – Dharman Apr 28 '19 at 10:50

1 Answers1

0

Checkboxes are only posted if they are checked. If they aren't checked, they won't be posted, which means that is_popup won't exist in your post array at all.

So to know if it's checked, just check if it exists:

$this_request_post["is_popup"] = $this_request_post["is_popup"] ?? 'N';

The above uses the null coalescing operator introduced in PHP 7.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40