Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Sql injection obstacle

Featured Replies

Hii everyone,

 

How to block sql injections and to preserve the code??

use mysqli_real_escape_string

 

It's not as simple as just using this function. You're going to have to do some real research. An example where mysqli_real_escape_string wont work:

$input = mysqli_real_escape_string($input);

SELECT * FROM table WHERE user_id=$input

 

Now, imagine $input is equal to "or user_id=1 and user_id !="

 

You're best off using prepared statements - they're designed for this very purpose, and do have some advantages (some disadvantages too, though)

 

Read more: http://php.net/manual/en/pdo.prepared-statements.php

 

Basically, prepared statements send the server a "framework" of your query. If you don't want to use prepared statements, you can just cast the input data to an integer, this way none of the above is possible. mysql_real_escape_string works on strings only (hint is in the name)

 

Good luck!

Edited by KieranA

It's not as simple as just using this function. You're going to have to do some real research. An example where mysqli_real_escape_string wont work:

$input = mysqli_real_escape_string($input);

SELECT * FROM table WHERE user_id=$input

 

Now, imagine $input is equal to "or user_id=1 and user_id !="

 

You're best off using prepared statements - they're designed for this very purpose, and do have some advantages (some disadvantages too, though)

 

Read more: http://php.net/manual/en/pdo.prepared-statements.php

 

Basically, prepared statements send the server a "framework" of your query. If you don't want to use prepared statements, you can just cast the input data to an integer, this way none of the above is possible. mysql_real_escape_string works on strings only (hint is in the name)

 

Good luck!

 

yep this is why i suggest writing queries like this SELECT * FROM table WHERE user_id='$input' with single quotes being put around the variable. I also suggest where people are taking integers such as a user id in input or the url is to filter everything else out but numbers yes i agree PDO is the best way to go but if you filter your data properly in the first place both ways can help prevent injection

Edited by webdesigner93

yep this is why i suggest writing queries like this SELECT * FROM table WHERE user_id='$input' with single quotes being put around the variable. I also suggest where people are taking integers such as a user id in input or the url is to filter everything else out but numbers yes i agree PDO is the best way to go but if you filter your data properly in the first place both ways can help prevent injection

 

Why would you put quotes around an integer? Prepared statements exist for a set of reasons. PDO != Prepared statements. They're totally different.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.