2017-03-01 37 views
0

我试图创建一个函数来过滤输入字段后提交 数据库连接PHP函数过滤

include("includes/connect.php"); 

这个函数就是这样

<?php 
function filter($x){ 
    global $conn; 
    $y=strip_tags(htmlspecialchars(htmlentities(mysqli_real_escape_string($conn,$x)))); 
} 
?> 

形式

<form method="post"> 
<input type="text" name="txt_name"> 
<input type="submit" name="add" value="Add">  
</form> 

代码

<?php 
if(isset($_POST['add'])){ 
    $name= filtration($_POST['txt_name']); 
    echo $name; 
} 
?> 

当我尝试打印过滤后的$名字我没有任何结果

我尝试这样做,

strip_tags(htmlspecialchars(htmlentities($x))) 

和我有一个输出

但对我来说我想用mysqli_real_escape_string

我该如何解决这个问题??!

回答

1

您需要从函数返回值。 :)

function filter($x) { 
    // do the cleaning of your string 
    return $y; 
} 
+0

坦克现在:)它的工作 – Developer

+0

不客气! –

0

更换

$name= filtration($_POST['txt_name']); 

$name= htmlspecialchars(filter_var(trim($_POST['txt_name']), FILTER_SANITIZE_STRING), ENT_QUOTES); 
+0

@mkl:谢谢...... – user1544541