为什么和addslashes()函数不是我在php阵列上运行? [英] Why does the addslashes() function not work on my array in php?
问题描述
我有一个数组一系列会话变量。当我在字符串变量中的一个使用引号,我试着和addslashes这样我就可以它最终插入到数据库,但和addslashes()函数无法正常工作。下面是一个例子。
在评论栏,我写这篇文章:
这是意见
块引用>我意识到这是一个问题,所以我增加了一个功能之前,我将其输入到通过一系列Session变量,包括评论运行数据库变量。
$ strip_fields = array($_SESSION['comments'],$_SESSION['employee_id'],$_SESSION['approved_by'],$_SESSION['delivery_email'],$_SESSION['full_name'],$_SESSION['first_name'],$_SESSION['last_name']); 的foreach($ strip_fields为$关键=> $值){
$键=和addslashes($键);
}我运行该功能后,我试着回声出意见变量$ _SESSION ['意见']
这是意见
块引用>所以,我可以看到,和addslashes功能不知何故不工作我使用它的方式。为什么和addslashes功能不能正常工作,我使用它的方式?
这是我的解决方案(我用了一下来自两个建议)
$ strip_fields =阵列(
'EMPLOYEE_ID','approved_by','delivery_email','FULL_NAME',
FIRST_NAME,姓氏,标题,TITLE_2','dept_div',
dept_div_2,电子邮件,评论,special_instructions
); 的foreach($ strip_fields为$键){
$ _SESSION [$关键] = $ conn-> real_escape_string($ _ SESSION [$关键]);
}
解决方案有几件事错在这里:
- 您正在复制在
$ _ SESSION
的值,以新的变量。- 您是路过的钥匙,
和addslashes()
,但您放置值放入数组值。的foreach()
复制该数组中的值到$键
和$值
,所以你对副本复印件运行。您应该能够使用引用这一点,但我觉得跳绳他们的将是更清晰。
$ strip_fields =阵列(
意见,雇员标识,approved_by','delivery_email',
FULL_NAME','FIRST_NAME','姓氏']
);的foreach($ strip_fields为$键){
$ _SESSION [$关键] =和addslashes($ _ SESSION [$关键]);
}I have a series of session variables in an array. When I use quotes in one of my string variables, I try to addslashes so I can eventually insert it into the DB, but the addslashes() function is not working. Here is an example.
In the comments field, I write this:
This is the "comment"
I realize this is a problem so I added a function before I enter it into the database that runs through a series of Session variables, including the comments variable.
$strip_fields = array($_SESSION['comments'],$_SESSION['employee_id'],$_SESSION['approved_by'],$_SESSION['delivery_email'],$_SESSION['full_name'],$_SESSION['first_name'],$_SESSION['last_name']); foreach($strip_fields as $key => $value) { $key = addslashes($key); }
After I run this function I try to echo out the comments variable $_SESSION['comments']
This is the "comment"
So I can see that the addslashes function somehow does not work the way I am using it. Why does the addslashes function not work the way I'm using it?
THIS IS MY SOLUTION (I used a bit from both suggestions)
$strip_fields = array( 'employee_id', 'approved_by', 'delivery_email', 'full_name', 'first_name', 'last_name', 'title', 'title_2', 'dept_div', 'dept_div_2', 'email', 'comments', 'special_instructions' ); foreach($strip_fields as $key) { $_SESSION[$key] = $conn->real_escape_string($_SESSION[$key]); }
解决方案There are a few things wrong here:
- You are copying the values in
$_SESSION
to new variables.- You are passing the keys to
addslashes()
, but you placed the values into array values.foreach()
copies the values from the array into$key
and$value
, so you're operating on a copy of a copy.You should be able to use references for this, but I think skipping them will be clearer.
$strip_fields = array( 'comments', 'employee_id', 'approved_by', 'delivery_email', 'full_name', 'first_name', 'last_name'] ); foreach($strip_fields as $key) { $_SESSION[$key] = addslashes($_SESSION[$key]); }
这篇关于为什么和addslashes()函数不是我在php阵列上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!