Jump to content

Best way to insert data into database


V

Recommended Posts

I saw a lot of ways to do this but I don't know which is best. I'm more conerned abot security issues.. The current method I'm using is this.

 

after the db connection functions I made variables for each form input

 

$movie_title=$_POST['movie_title'];
$movie_desc=$_POST['movie_desc'];
$genre_id=$_POST['genre_id'];

 

and then the insert

 

$sql="INSERT INTO movies (movie_title, movie_desc, genre_id)
VALUES
('".$movie_title."', '".$movie_desc."', '".$genre_id."')";

 

 

It works but it feels like it's missing some things. What are your thoughts?

Link to comment
Share on other sites

It is not. All it does is it applies addslashes to every variable in GET, POST and COOKIE arrays. And since addslashes() is NOT as secure as dedicated mysql function (or functions dedicated to other databases), it forces you to apply stripslashes and then escape these variables properly. Failing to do so will result in double escaping (slashes stored into database).

Link to comment
Share on other sites

No, it's not.

 

It will escape all quotes on Strings that are passed via either GET, POST or COOKIEs (gpc).

This will protect you against the most basic forms of SQL injection, but it's easily circumvented.

But what happens if your host decides to switch magic_quotes off? What if you move your script to a server where it's disabled by default? Your script will fail.

 

Now, if you use mysql_real_escape_string in combination with magic_quotes, you have an effect that you most likely don't want.

 

Consider the following:

You have a basic text form on your page, and a user writes "it's" (double quotes indicate the start and the end of the string)

When you, in PHP get to see the value, magic_quotes will have changed it to:

"it\'s"

When you then pass it to mysql_real_escape_string, you will get

"it\\\'s" (escaping the backslash with another \, and it also escapes the single quote once more)

mysql_real_escape_string allows you to store "it\'s" in its actual form in the database, and that's exactly what you'll get when you query it: "it\'s".

 

In the end, you have escaped your data twice.

 

As of PHP6, magic_quotes has been removed entirely. It was introduced to prevent SQL injections because a lot of casual coders were unaware of the dangers of using unprocessed input. As a result, new PHP users didn't even know that feature existed, and what it was for. They simply assumed you could safely put anything into a query, which lead to numerous security issues when the magic_quotes setting was changed.

Another such "safety" feature is register_globals, which is due to be removed as well.

Link to comment
Share on other sites

I use a function when inserting data, to what I call, "sanitize" the data.

 

      function sanitize($string, $trim = false)
      {
          $string = filter_var($string, FILTER_SANITIZE_STRING);
          $string = trim($string);
          $string = stripslashes($string);
          $string = strip_tags($string);
          $string = str_replace(array('‘', '’', '“', '”'), array("'", "'", '"', '"'), $string);
          if ($trim)
              $string = substr($string, 0, $trim);
              
          $string = mysql_real_escape_string($string);
          
          return $string;
      }

Link to comment
Share on other sites

I use a function when inserting data, to what I call, "sanitize" the data.

 

      function sanitize($string, $trim = false)
      {
          $string = filter_var($string, FILTER_SANITIZE_STRING);
          $string = trim($string);
          $string = stripslashes($string);
          $string = strip_tags($string);
          $string = str_replace(array('‘', '’', '“', '”'), array("'", "'", '"', '"'), $string);
          if ($trim)
              $string = substr($string, 0, $trim);
              
          $string = mysql_real_escape_string($string);
          
          return $string;
      }

Don't overdo things:

filter_var($string, FILTER_SANITIZE_STRING);

already strips tags and enocdes any quotes, so there's no need to do that again.

You should only use stripslashes if you know there are slashes to strip. Doing this every time you're risking removing slashes that should be there (if this was for a forum for example, it would be impossible to input c:\windows\style\paths or \php\namespaces )

Link to comment
Share on other sites

filter_var($string, FILTER_SANITIZE_STRING);

 

I never understood why you would sanitize input at all? Really, you are only helping the attacker in his efforts. If the data isn't what you expected like a piece of text where you expected a number or a negative where you expected a positive number, then just reject it. The end-user is messing with the variables, shut him up.

Link to comment
Share on other sites

These are great tips thanks everyone! :) @Phantom thanks for the example I understand the use now.

 

Should one also use any injection prevention with forms that send email but don't store anything in the database?

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.