Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Uniwersalna funkcja do html decode
trifek
post
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


Witam.
Mam tablice:

  1. (
  2. [save] => 1
  3. [title] => Monika Krzysiowa
  4. [contactUser] => Array
  5. (
  6. [0] => 29
  7. [1] => 11
  8. [2] => 17
  9. )
  10.  
  11. [email] => kontakt@mail.com
  12. [phone] => 12345678
  13. [description] => opis
  14. 1
  15. 2
  16. 3
  17. 4
  18. [enable] => 1
  19. )



lub



  1. (
  2. [save] => 1
  3. [title] => Monika Krzysiowa
  4. [email] => kontakt@gmail.com
  5. [phone] => 12345678
  6. [description] => opis
  7. 1
  8. 2
  9. 3
  10. 4
  11. [enable] => 1
  12. )



i taką funkcję w php:
  1. public function secureSave(array $string): array
  2. {
  3. foreach ($string as $key => $value) {
  4. $string[$key] = htmlspecialchars_decode($value, ENT_COMPAT);
  5. }
  6. return $string;
  7. }



Powyższa funkcja pracuje poprawnie dla 2 tablicy. W 1 mam problem i daje wynik:
  1. (
  2. [save] => 1
  3. [title] => Monika Krzysiowa
  4. [contactUser] =>
  5. [email] => kontakt@gmaail.com
  6. [phone] => 123456788
  7. [description] => opis
  8. 1
  9. 2
  10. 3
  11. 4
  12. [enable] => 1
  13. )


[contactUser] - problemem jest pusta wartość.

Jak naprawić powyższą funkcję tak żeby była uniwersalna?

Mam różne tablice, nazwy mogą być różne
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
trifek
post
Post #2





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


Hmmmm.... tylko jak nie dodaję tego "zabezpieczenia" to taki wpis:

'';fwefewfpew'f'wef'wefew.''fewvdsniu*&&^&^@^7ef125e2'



wywala mi błąd:

  1. Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'f'wef'wefew.''fewvdsniu*&&^&^@^7ef125e2''' at line 1 in /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/core/utilities/DbClass.php:78 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/core/utilities/DbClass.php(78): PDOStatement->execute() #1 /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/core/utilities/DbClass.php(139): Core\Utilities\Db->Init('SELECT COUNT(ti...', NULL) #2 /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/core/models/ModelClass.php(24): Core\Utilities\Db->row('SELECT COUNT(ti...') #3 /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/backend/models/GalleryModel.php(230): Core\Models\Model->createSeoUrl(''';fwefewfpew'f...', 'title_pl', 'psGalleryCatego...') #4 /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/backend/controllers/GalleryList.php(14 in /Applications/XAMPP/xamppfiles/htdocs/um_rumia/apps/core/utilities/DbClass.php on line 78




Moja klasa od połączeń wygląda tak:

  1. class Db
  2. {
  3. private $_hostname;
  4. private $_database;
  5. private $_username;
  6. private $_password;
  7. private $_port;
  8. private $_pdo;
  9. private $_sQuery;
  10. private $_bConnected = false;
  11. private $_parameters;
  12. private $_config;
  13. private $_psException;
  14.  
  15.  
  16. public function __construct()
  17. {
  18. $this->_config = Registry::register("Core\Utilities\Config");
  19. $this->_psException = new PsException();
  20.  
  21. $this->_hostname = $this->_config->db_host;
  22. $this->_database = $this->_config->db_db;
  23. $this->_username = $this->_config->db_user;
  24. $this->_password = $this->_config->db_pass;
  25. $this->_port = $this->_config->db_port;
  26.  
  27. $this->Connect($this->_hostname, $this->_database, $this->_username, $this->_password, $this->_port);
  28. $this->_parameters = array();
  29. }
  30.  
  31.  
  32. private function Connect($hostname, $database, $username, $password, $port)
  33. {
  34.  
  35. try {
  36. $options = array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'");
  37. $this->_pdo = new \PDO("mysql:host={$hostname};dbname={$database};port={$port};charset=utf8", $username, $password, $options);
  38.  
  39. $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  40. $this->_pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
  41. $this->_pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
  42. $this->_pdo->query('SET NAMES utf8');
  43. $this->_bConnected = true;
  44. } catch (PDOException $ex) {
  45. $this->_psException->registerError("Failed to connect to the database: " . $ex->getMessage());
  46. } catch (Exception $e) {
  47. $this->_psException->registerError("Failed to connect to the database: " . $e->getCode() . "." . $e->getMessage());
  48. }
  49. }
  50.  
  51.  
  52. public function CloseConnection()
  53. {
  54. $this->_pdo = null;
  55. }
  56.  
  57.  
  58. private function Init($query, $parameters = "")
  59. {
  60. if (!$this->_bConnected) {
  61. $this->Connect();
  62. }
  63. try {
  64. $this->_sQuery = $this->_pdo->prepare($query);
  65.  
  66. $this->bindMore($parameters);
  67. if (!empty($this->_parameters)) {
  68. foreach ($this->_parameters as $param) {
  69. $parameters = explode("\x7F", $param);
  70. $this->_sQuery->bindParam($parameters[0], $parameters[1]);
  71. }
  72. }
  73. $this->success = $this->_sQuery->execute();
  74. } catch (PDOException $e) {
  75. $this->ExceptionLog($e->getMessage(), $query);
  76. }
  77. $this->_parameters = array();
  78. }
  79.  
  80.  
  81. public function bind($para, $value)
  82. {
  83. $this->_parameters[sizeof($this->_parameters)] = ":" . $para . "\x7F" . ($value);
  84. }
  85.  
  86. public function bindMore($parray)
  87. {
  88. if (empty($this->_parameters) && is_array($parray)) {
  89. $columns = array_keys($parray);
  90. foreach ($columns as $i => &$column) {
  91. $this->bind($column, $parray[$column]);
  92. }
  93. }
  94. }
  95.  
  96. public function query($query, $params = null, $fetchmode = \PDO::FETCH_ASSOC)
  97. {
  98. $query = trim($query);
  99. $this->Init($query, $params);
  100. $rawStatement = explode(" ", $query);
  101.  
  102. $statement = strtolower($rawStatement[0]);
  103.  
  104. if ($statement === 'select' || $statement === 'show') {
  105. return $this->_sQuery->fetchAll($fetchmode);
  106. } elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
  107. return $this->_sQuery->rowCount();
  108. } else {
  109. return null;
  110. }
  111. }
  112.  
  113. public function lastInsertId()
  114. {
  115. return $this->_pdo->lastInsertId();
  116. }
  117.  
  118.  
  119. public function column($query, $params = null)
  120. {
  121. $this->Init($query, $params);
  122. $Columns = $this->_sQuery->fetchAll(\PDO::FETCH_NUM);
  123.  
  124. $column = null;
  125. foreach ($Columns as $cells) {
  126. $column[] = $cells[0];
  127. }
  128. return $column;
  129.  
  130. }
  131.  
  132. public function row($query, $params = null, $fetchmode = \PDO::FETCH_ASSOC)
  133. {
  134. $this->Init($query, $params);
  135. return $this->_sQuery->fetch($fetchmode);
  136. }
  137.  
  138. public function single($query, $params = null)
  139. {
  140. $this->Init($query, $params);
  141. return $this->_sQuery->fetchColumn();
  142. }
  143.  
  144. private function ExceptionLog($message, $sql = "")
  145. {
  146. $message .= 'Unhandled Exception. $message';
  147. if (!empty($sql)) {
  148. $message .= "\r\nQuery SQL : " . $sql;
  149. }
  150. $this->_psException->registerError($message);
  151. }
  152. }




Czyli błąd jest po stronie Db?
Go to the top of the page
+Quote Post

Posty w temacie
- trifek   [PHP]Uniwersalna funkcja do html decode   27.02.2019, 09:43:16
- - nospor   W momemecie gdy wartoscia jest kolejna tablica, to...   27.02.2019, 10:00:33
- - trifek   Czyli coś takiego: [PHP] pobierz, plaintext $stri...   27.02.2019, 10:14:15
- - nospor   Napisz ten kod jeszcze raz bo nie ma prawa sie w o...   27.02.2019, 10:25:16
- - trifek   [PHP] pobierz, plaintext public function secureSav...   27.02.2019, 10:33:31
- - nospor   CytatW jaki sposób sugerujesz żeby kodować html pr...   27.02.2019, 10:36:55
- - trifek   Hmmmm.... tylko jak nie dodaję tego "zabezpie...   27.02.2019, 10:42:39
- - nospor   Jak dla mnie to ta cala twoja klasa jest do przepi...   27.02.2019, 10:46:55
- - trifek   jak debuguję tutaj: [PHP] pobierz, plaintext $this...   27.02.2019, 12:14:38
- - nospor   Jak sam widzisz twoje zapytanie zawiera tekst ktor...   27.02.2019, 12:17:35
- - trifek   [PHP] pobierz, plaintext $queryValue["ena...   27.02.2019, 13:21:06
- - nospor   pokaz co zwraca: [PHP] pobierz, plaintext ...   27.02.2019, 13:26:28
- - trifek   [PHP] pobierz, plaintext array(2) { ...   27.02.2019, 13:31:24
- - nospor   Kurcze, jako przyklad podales mi zapytanie z INSER...   27.02.2019, 13:45:00
- - trifek   Przepraszam bardzo. [PHP] pobierz, plaintext array...   27.02.2019, 13:53:26
- - nospor   No i poprosze jeszcze jak to wywolujesz z tym zapy...   27.02.2019, 14:20:00
- - trifek   Selecty tak wywołuję: [PHP] pobierz, plaintext p...   27.02.2019, 22:17:52
- - nospor   $searchQuery .= " and ($searchNames...   28.02.2019, 09:48:45


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 15.10.2025 - 13:12