Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么mysql\u real\u escape\u字符串在此代码中不起作用?_Php_Mysql Real Escape String - Fatal编程技术网

Php 为什么mysql\u real\u escape\u字符串在此代码中不起作用?

Php 为什么mysql\u real\u escape\u字符串在此代码中不起作用?,php,mysql-real-escape-string,Php,Mysql Real Escape String,为什么mysql\u real\u escape\u string不能在这个代码中工作 通常,当加载页面时,输入将如下所示 http://image.free.in.th/v/2013/ie/160812064246.jpg http://image.free.in.th/v/2013/ia/160812064312.jpg 但是当您加载页面www.example.com/test.php?value_1=“> 为什么输入看起来像这样 http://image.free.in.th/v/20

为什么
mysql\u real\u escape\u string
不能在这个代码中工作

通常,当加载页面时,输入将如下所示

http://image.free.in.th/v/2013/ie/160812064246.jpg
http://image.free.in.th/v/2013/ia/160812064312.jpg
但是当您加载页面
www.example.com/test.php?value_1=“>

为什么输入看起来像这样

http://image.free.in.th/v/2013/ie/160812064246.jpg
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); ">


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

这就是语法
mysqli\u real\u escape\u字符串(连接、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);
?>

mysql\u real\u escape\u string
将数据转义,以便您可以安全地将其放入SQL查询中,然后发送到mysql。(注意:)

value=“”
这不是SQL,而是HTML,您可以将其发送到web浏览器

HTML与SQL是不同的语言。转义规则是不同的,甚至没有细微的区别,它们是完全不同的


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

你真的不应该再使用mysql API了。你必须在
mysql\u real\u escape\u string
中传递连接变量,这绝对是错误的,
mysql\u real\u escape\u string
可能是有史以来最糟糕的事情,但它只需要1个参数,而不是2个。这里使用的函数是
mysql基于