error during addslashes() function in php

4.5k Views Asked by At

html form code-

<td width="75">
<input name="txtQty[]" type="text" id="txtQty[]" size="5" 
 value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">

when I submit form I calls following script-

if (!get_magic_quotes_gpc()) {
if (isset($_POST)) {
    foreach ($_POST as $key => $value) {
        $_POST[$key] =  trim(addslashes($value));
    }
}

if (isset($_GET)) {
    foreach ($_GET as $key => $value) {
        $_GET[$key] = trim(addslashes($value));
    }
}   
}

error-

Warning: addslashes() expects parameter 1 to be string, array given in C:\xampp\htdocs\shizin\products\library\config.php on line 53

I think this script is being used just to trim input but I dont know what this addslash function does and why this error coming.

4

There are 4 best solutions below

0
Haim Evgi On

the error said , the addslashes function try to Quote string with slashes , but the $value the parameter is not a string is an array, what CONTAIN the $_GET ?

its because that the page that call to this script pass a array . txtQty[]

http://php.net/manual/en/function.addslashes.php

0
Mononofu On

Just echo $value before passing it to addslashes(), then you should see the problem immediately.

0
Your Common Sense On
  1. The whole approach is wrong.
    Upon receiving user supplied data you have to strip slashes, added by magic quotes, not add.

  2. About array approach it says 2 answers already posted, I hope it is well explained here. Not so well, but anyway.

So, you will need 2 code snippets.
A first one is stripslashes_deep() from http://www.php.net/manual/en/function.stripslashes.php

A second one you will get after you tell us, why did you think you need the code you posted.

1
Chandraveer singh On

If you apply this code on an int value then you remove these function like this

if (!get_magic_quotes_gpc()) { 
if (isset($_POST)) { 
    foreach ($_POST as $key => $value) { 
        $_POST[$key] =  $value; 
    } 
} 

if (isset($_GET)) { 
    foreach ($_GET as $key => $value) { 
        $_GET[$key] = $value; 
    } 
}    
}