<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":3892,"date":"2022-01-29T11:09:56","date_gmt":"2022-01-29T11:09:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=3892"},"modified":"2022-01-29T11:09:56","modified_gmt":"2022-01-29T11:09:56","slug":"how-to-prevent-sql-injection-in-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/","title":{"rendered":"How to prevent SQL injection in PHP ?"},"content":{"rendered":"<p style=\"text-align: justify;\">If user input is inserted without modification into an SQL query, then the application becomes vulnerable to\u00a0SQL injection, like in the following example:<\/p>\n<pre class=\"lang-php s-code-block\"><code class=\"hljs language-php\"><span class=\"hljs-variable\">$unsafe_variable<\/span> = <span class=\"hljs-variable\">$_POST<\/span>[<span class=\"hljs-string\">'user_input'<\/span>]; \r\n\r\nmysql_query(<span class=\"hljs-string\">\"INSERT INTO `table` (`column`) VALUES ('<span class=\"hljs-subst\">$unsafe_variable<\/span>')\"<\/span>);\r\n<\/code><\/pre>\n<p style=\"text-align: justify;\">That&#8217;s because the user can input something like\u00a0<code>value'); DROP TABLE table;--<\/code>, and the query becomes:<\/p>\n<pre class=\"default s-code-block\"><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> `<span class=\"hljs-keyword\">table<\/span>` (`<span class=\"hljs-keyword\">column<\/span>`) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'value'<\/span>); <span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">table<\/span>;<span class=\"hljs-comment\">--')<\/span>\r\n<\/code><\/pre>\n<p style=\"text-align: justify;\">What can be done to prevent this from happening ?<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution 1<\/strong><\/p>\n<p>You basically have two options to achieve this:<\/p>\n<ol>\n<li>Using\u00a0<strong>PDO<\/strong>\u00a0(for any supported database driver):<\/li>\n<\/ol>\n<pre class=\"lang-php s-code-block\"><code class=\"hljs language-php\"><span class=\"hljs-variable\">$stmt<\/span> = <span class=\"hljs-variable\">$pdo<\/span>-&gt;prepare(<span class=\"hljs-string\">'SELECT * FROM employees WHERE name = :name'<\/span>);\r\n\r\n <span class=\"hljs-variable\">$stmt<\/span>-&gt;execute([ <span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-variable\">$name<\/span> ]);\r\n\r\n <span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-variable\">$stmt<\/span> <span class=\"hljs-keyword\">as<\/span> <span class=\"hljs-variable\">$row<\/span>) {\r\n     <span class=\"hljs-comment\">\/\/ Do something with $row<\/span>\r\n }<\/code>\r\n\r\n\r\n2. Using <strong>MySQLi<\/strong>\u00a0(for MySQL):\r\n\r\n<\/pre>\n<pre class=\"lang-php s-code-block\"><code class=\"hljs language-php\"><span class=\"hljs-variable\">$stmt<\/span> = <span class=\"hljs-variable\">$dbConnection<\/span>-&gt;prepare(<span class=\"hljs-string\">'SELECT * FROM employees WHERE name = ?'<\/span>);\r\n <span class=\"hljs-variable\">$stmt<\/span>-&gt;bind_param(<span class=\"hljs-string\">'s'<\/span>, <span class=\"hljs-variable\">$name<\/span>); <span class=\"hljs-comment\">\/\/ 's' specifies the variable type =&gt; 'string'<\/span>\r\n\r\n <span class=\"hljs-variable\">$stmt<\/span>-&gt;execute();\r\n\r\n <span class=\"hljs-variable\">$result<\/span> = <span class=\"hljs-variable\">$stmt<\/span>-&gt;get_result();\r\n <span class=\"hljs-keyword\">while<\/span> (<span class=\"hljs-variable\">$row<\/span> = <span class=\"hljs-variable\">$result<\/span>-&gt;fetch_assoc()) {\r\n     <span class=\"hljs-comment\">\/\/ Do something with $row<\/span>\r\n }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If user input is inserted without modification into an SQL query, then the application becomes vulnerable to\u00a0SQL injection, like in the following example: $unsafe_variable = $_POST[&#8216;user_input&#8217;]; mysql_query(&#8220;INSERT INTO `table` (`column`) VALUES (&#8216;$unsafe_variable&#8217;)&#8221;); That&#8217;s because the user can input something like\u00a0value&#8217;); DROP TABLE table;&#8211;, and the query becomes: INSERT INTO `table` (`column`) VALUES(&#8216;value&#8217;); DROP TABLE table;&#8211;&#8216;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[16540],"tags":[],"class_list":["post-3892","post","type-post","status-publish","format-standard","hentry","category-cyber-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to prevent SQL injection in PHP ?<\/title>\n<meta name=\"description\" content=\"How to prevent SQL injection in PHP ?\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to prevent SQL injection in PHP ?\" \/>\n<meta property=\"og:description\" content=\"How to prevent SQL injection in PHP ?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-29T11:09:56+00:00\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/\",\"name\":\"How to prevent SQL injection in PHP ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2022-01-29T11:09:56+00:00\",\"dateModified\":\"2022-01-29T11:09:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"How to prevent SQL injection in PHP ?\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\/\/www.wikitechy.com\/interview-questions\"],\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to prevent SQL injection in PHP ?","description":"How to prevent SQL injection in PHP ?","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How to prevent SQL injection in PHP ?","og_description":"How to prevent SQL injection in PHP ?","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/","og_site_name":"Wikitechy","article_published_time":"2022-01-29T11:09:56+00:00","author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/","name":"How to prevent SQL injection in PHP ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2022-01-29T11:09:56+00:00","dateModified":"2022-01-29T11:09:56+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"How to prevent SQL injection in PHP ?","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/cyber-security\/how-to-prevent-sql-injection-in-php\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/3892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=3892"}],"version-history":[{"count":1,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/3892\/revisions"}],"predecessor-version":[{"id":3893,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/3892\/revisions\/3893"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=3892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=3892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=3892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}