Jump to content

cleaning user inout before storing it into db


stribor40

Recommended Posts

Once I collect user input from my form I have to insert it into database. What is proper way of cleaning the data before insertion into db.

Does it have to be done both client and server side?

What kind of function have to be applied to each iput?

All my inputs are basically dates,numbers and message boxes. I noticed that when I try to isert that contains single quote i receive error from ms sql server. Obsviosly I would have to replace single quotes. How do i replace them and how do i add single qoute when i retrieve record from database.

Link to comment
Share on other sites

Use PDO and prepared statements.  You probably should check your data to make sure its the proper type (int, date, etc) but prepared statements will escape your data and make sure things like single quote/apostrophe are escaped correctly and that escaping will also escape out things in a SQL injection attempt.

Link to comment
Share on other sites

to clean number you just need to do like that:

$x = (int) $_POST['somenumber'];

to work with string type try to use addslashes function (though i am not familiar with odbc driver. You just try).

how do i add single qoute when i retrieve record from database

 

you would not need to.

Link to comment
Share on other sites

I also am not sure how ODBC handles data, but you should probably look at the ODBC driver for PDO as it may have escaping built-in.  I do know that for MS SQL that to escape a single quote ' you just add an additional single quote '' (two single quotes), so a query would look like:

SELECT * FROM table_name WHERE lastname = 'O''reilly'

There are other characters that may need to be escaped and that would depend on the database being used and if the ODBC driver changes the requirements.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.