Injection (Php-Mysql)

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

baris22

Established Member
Impact
1
I have got a problem with a character. I do not know which one it is. I am inserting long text files into database and sometime i can not insert. Any idea which character can it be?

I am using this code

PHP:
$links.=$_POST['links'][$i];
				$description=$_POST['description'][$i];
				$type=$_POST['type'];
				if (!get_magic_quotes_gpc()) {
					$title=addslashes($title);
					$links=addslashes($links);
					$description=addslashes($description);
				}
				$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
			  $title = str_replace($to_replace, " ", $title);
			  $description = str_replace($to_replace, " ", $description);
				//echo $title . '<br/>';
				$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
				mysql_query($query);
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
The easy way to fix this problem is to use mysql_real_escape_string() on your input this will ensure that any relevant characters are escaped.

Oh and by the way stop using addslashes it is far from ideal, if you use the above mentioned function then the addslashes function is not needed (and can cause problems) anyway.

I would also check and see if you have magic_quotes_gpc enabled (most hosts do). If that is the case reverse what this feature does or disable it using a .htaccess file if you are able too. In fact while you are at it disable register_globals.
 
0
Close
PHP:
 $links.=$_POST['links'][$i]; 
                $description=$_POST['description'][$i]; 
                $type=$_POST['type']; 
                if (!get_magic_quotes_gpc()) { 
                    $title=mysql_real_escape_string($title); 
                    $links=mysql_real_escape_string($links); 
                    $description=mysql_real_escape_string($description); 
                } 
                $to_replace = array("\r\n","|","\n","\\r\\n","\\n"); 
              $title = str_replace($to_replace, " ", $title); 
              $description = str_replace($to_replace, " ", $description); 
                //echo $title . '<br/>'; 
                $query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')"; 
                mysql_query($query);

is it how to use mysql_real_escape_string?

thanks
 
0
Close
yes thats fine, just a note I do not know what the content of $type should be but you should run the function on that as well.
 
0
Close
it did not work. Same problem.

then i used this code but same problem again. I do not know if this code is ok

PHP:
$links.=$_POST['links'][$i];
				$description=$_POST['description'][$i];
				$type=$_POST['type'];
				
				$title=stripslashes($title);
				$links=stripslashes($links);
				$description=stripslashes($description);
				
				$title=mysql_real_escape_string($title);
				$links=mysql_real_escape_string($links);
				$description=mysql_real_escape_string($description);
				
				$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
			  $title = str_replace($to_replace, " ", $title);
			  $description = str_replace($to_replace, " ", $description);
				//echo $title . '<br/>';
				$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
				mysql_query($query);
				echo mysql_error();
 
0
Close
echo out the query and post it here, also a copy of the error message you receive.

Ahh and just noticed. Do the mysql_real_escape_string lines after the following not before, i never noticed it before:-

PHP:
                $to_replace = array("\r\n","|","\n","\\r\\n","\\n");
              $title = str_replace($to_replace, " ", $title);
              $description = str_replace($to_replace, " ", $description);
 
0
Close
This is the code i used

PHP:
$links.=$_POST['links'][$i];
				$description=$_POST['description'][$i];
				$type=$_POST['type'];
				if (!get_magic_quotes_gpc()) {
					$title=addslashes($title);
					$links=addslashes($links);
					$description=addslashes($description);
				}
			  $to_replace = array("\r\n","|","\n","\\r\\n","\\n");
			  $title = str_replace($to_replace, " ", $title);
			  $description = str_replace($to_replace, " ", $description);
			  
			  $title=stripslashes($title); 
              $description=stripslashes($description);
				
			  $title=mysql_real_escape_string($title);
			  $description=mysql_real_escape_string($description);
				//echo $title . '<br/>';
				$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
				mysql_query($query) OR die(mysql_error());
				echo mysql_error();

I do not get an error. I get this

Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
 
0
Close
That seems completely unrelated.
 
0
Close
i think the problem is on one character. I have to add 50 entries at once. It works 90%.

I added over 100 pages. 100x50. it worked about 90 pages. I am just going to try htmlspecialchars
 
0
Close
invalid characters should not cause a Service Temporarily Unavailable if it does then there is something seriously wrong.
 
0
Close
I know. I need to check every single entry to see what is wrong.

Is there anything i can add to

$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
 
0
Close
Why are you replacing them with a space? They will not be causing you any problem and in fact you are losing formatting by doing this.
 
0
Close
No, i have to do that. My code graps content from other web sites.

Do you think this can happen because of the charset. If there is a character which is not ok. with mysql settings?
 
0
Close
it shouldn't do, mysql_real_escape_string takes into account the charset that is in use. Anyway if the charset was the problem you should see a mySQL error being generated.
 
Last edited:
0
Close
Oh my god. I found the answer after 2 long days.

When i try to add the word "wget " to database it does not work.

I am so serious. When i try to add "wget " (there is a space after) it does not work.
 
0
Close
If you'd rather not have the space at the end, you could try using rtrim() (and other related functions) to remove spaces from the end of a string.
 
0
Close
I think i am going to use

PHP:
$to_replace = array("\r\n","|","wget ","\n","\\r\\n","\\n");
			  $title = str_replace($to_replace, " ", $title);
			  $description = str_replace($to_replace, " ", $description);


I do not think it will be a problem.
 
0
Close
OK, good luck with the code.
 
0
Close
the_internet said:
OK, good luck with the code.

it did not work

PHP:
 $to_replace = array("\r\n","|","wget ","\n","\\r\\n","\\n"); 
              $title = str_replace($to_replace, " ", $title); 
              $description = str_replace($to_replace, " ", $description);
 
0
Close
i cant see why having the string wget would cause problems unless you were running it through exec or something like that.
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Member card: see how many threads a member started
The member card now shows a Threads number, right after Posts. The member card is the box that opens when you point at a name. Profile...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back