2016-08-12 183 views
-1

为什么mysql_real_escape_string不能在这段代码中工作?为什么mysql_real_escape_string在这段代码中不起作用?

正常情况下,当加载页面输入将看起来像这样。

http://image.free.in.th/v/2013/ie/160812064246.jpg 

但是当你加载www.example.com/test.php?value_1=">

页为什么输入这个样子的。

http://image.free.in.th/v/2013/ia/160812064312.jpg 

我该怎么办?

test.php的

<?PHP 
include("connect.php"); 
$ex_search_value = mysql_real_escape_string($_GET['value_1']); 
?> 

<input placeholder="PLEASE ENTER" value="<?PHP echo $ex_search_value; ?>" style=" margin: 0px; width: 810px;height: 33px;line-height: 17px;line-height: 33px\0;^line-height: 33px;padding: 0px;margin-right: -1px;padding-left: 5px;-webkit-box-shadow: 0 2px 3px rgba(0,0,0,0.1);-moz-box-shadow: 0 2px 3px rgba(0,0,0,0.1); box-shadow: 0 2px 3px rgba(0,0,0,0.1); "> 
+1

你真的不应该再使用mysql API了 – 2016-08-12 06:40:38

+0

你必须在'mysql_real_escape_string'中传递连接变量 – Saurabh

+0

[伟大的逃避现实(或:你需要知道如何处理文本中的文本)](http:// kunststube.net/escapism/) – deceze

回答

-2

MySQL的API已被弃用,你真的不应该用它尝试使用mysqli的

这句法 mysqli_real_escape_string(connection,escapestring); 它需要连接对象和字符串你想逃离

一个很好的例子是这样的

<?php 
    $con=mysqli_connect("localhost","my_user","my_password","my_db"); 
    // Check connection 
    if (mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    // escape variables for security 
    $firstname = mysqli_real_escape_string($con, $_POST['firstname']); 
    $lastname = mysqli_real_escape_string($con, $_POST['lastname']); 
    $age = mysqli_real_escape_string($con, $_POST['age']); 

    $sql="INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES ('$firstname', '$lastname', '$age')"; 

    $query = mysqli_query($con, $sql); 

    if (!mysqli_query($con,$sql)) { 
    die('Error: ' . mysqli_error($con)); 
    } 
    echo "1 record added"; 
    mysqli_close($con); 
?> 
+1

绝对错误,'mysql_real_escape_string'可能是最糟糕的事情,但它只需要1个参数,而不是2。这里使用的函数是基于'mysqli' –

1

mysql_real_escape_string转义数据,以便您可以安全地将它放入SQL查询中,然后发送给MySQL。

value="<?PHP echo $ex_search_value; ?>" 

这不是SQL:(NB mysql_real_escape_string is part of an obsolete API you should have stopped using about half a decade ago)。这是您发送到Web浏览器的HTML。

HTML是与SQL不同的语言。逃避的规则是不同的,甚至没有微妙的差别,它们完全不同。

您需要使用htmlspecialchars()才能转义数据,因此适合插入到HTML中。

相关问题