English 中文(简体)
Problems with space within parameters of a php function
原标题:

Sorry for not formatting my code. the toolbar was gone...

I want to insert some data into a mysql db. I ve wrote a function in php:

function add_ID($ID, $token)  {
 $add = "INSERT INTO ids (ID, token) VALUES ( $ID ,  $token )";
 mysql_query($add);
 echo  added successfully ;
}  
if(isset($_GET[ addDeviceID ])) {
 add_ID($_GET[ ID ], $_GET[ token ]);
}

In the URL-Field of my Browswe I am calling the function like that: http://www.justanexample.com/example.php?ID=123123123&token=qwertzuiop

That works.

If I put a space into either one of the parameters for example like that: http://www.justanexample.com/example.php?ID=123123 123&token=qwertzuiop

Nothing was added to my mysql db.

Would be great to get some help :) Thank you!

最佳回答

Your function is vulnerable to SQL injection. You should validate all user-received parameters before using them in an SQL query, and pass any strings through mysql_real_escape_string, because then I could just pass in something like example.php?token= ; DROP DATABASE; and royally screw up your application.

In your case, you should make a check that the received parameters are in the form that you expect first, return an error to the user if they don t, and only then pass them into the SQL query.

function add_ID($ID, $token)  {
  $id = mysql_real_escape_string($id);
  $token = mysql_real_escape_string($token);

  $add = "INSERT INTO ids (ID, token) VALUES ( $ID ,  $token )";
  mysql_query($add);
  echo  added successfully ;
}  

if(isset($_GET[ addDeviceID ])) {
  $id    = isset($_GET[ id ]) ? $_GET[ id ] : 0; // in case no ID has been passed in
  $token = isset($_GET[ token ]) ? $_GET[ token ] :   ;

  if (!is_numeric($id) {
    die( ID is not a number );
  } 

  // validate token here as well

  add_ID($id, $token);
}

You should also look into parametrized queries, which are an overall much better way of doing SQL queries with parameters than just using string concatenation. For that, look into using the mysqli extension instead of mysql, or at a higher level, PDO.

问题回答

You should validate your input before sending it to the database. Or, if validation is not possible, filter and/or escape the value.

Validation

If you expect ID to be a integer greater than zero:

if (!ctype_digit($ID)) {
    // invalid ID
}

If you expect token to be an alphanumeric string:

if (!ctype_alnum($token)) {
    // invalid token
}

Filtering

Filtering is removing invalid parts of the input so that it becomes valid:

if (!ctype_digit($ID)) {
    $ID = preg_replace( /D+/ ,   , $ID);
    // $ID does now only contain digits
}
if (!ctype_alnum($token)) {
    $token = preg_replace( /D+/ ,   , $token);
    // $token does now only contain alphanumeric characters
}

Escaping

Escaping is replacing the meta characters of a specific context some string is meant to be placed in. For MySQL queries you should use a function that escapes the meta characters of the context string declaration in MySQL. PHP has the mysql_real_escape_string function for that purpose:

$add = "INSERT INTO ids (ID, token) VALUES ( ".mysql_real_escape_string($ID)." ,  ".mysql_real_escape_string($token)." )";

Remove space from them using str_replace function eg:

 $ID = str_replace(   ,   , $ID);
 $token= str_replace(   ,   , $token);

 $add = "INSERT INTO ids (ID, token) VALUES ( $ID ,  $token )";

Also, I suspect your $ID is a integer field in your table so you can run your query without specifying quotes eg:

 $add = "INSERT INTO ids (ID, token) VALUES ($ID,  $token )";

Your code is assuming the query successfully completes without ever checking if there was an error. I m guessing it ll be a syntax error due to the spaces. If your ID field is an integer type, then doing ID=123 123 will be the syntax error. Including all the SQL injection and data sanitizing advice in the other answers, you should rewrite your add_ID function as follows:

function add_ID($ID, $token) {
  $query =  blah blah blah ;
  mysql_query($query);
  if (mysql_error()) {
       echo  ruhroh, someone set us up the bomb:  , mysql_error();
  } else {
       echo  woohoo, it worked! ;
  }
}

At least this will tell you if the query REALLY did succeed, and what blew up if it didn t. Never assume that a database query of any sort will succeed. There s far too many ways for it to blow up (server died, transaction deadlock, connection pool exhausted, out of disk space, etc...) to NOT have even some simplistic error handling as above.

You can use str_replace to remove spaces. But it s not a good practice. How can URL s be modified so? In normal cases it s unreal. Contrary, you should test all input values from user(ID must be an integer, Token shouldn t contains " " symbol and other checks). Read about sql-injections.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签