简体   繁体   English

PHP mysqli_real_escape_string返回空字符串

[英]PHP mysqli_real_escape_string returning empty string

The code works fine if I don't use the mysql_real_escape_string function. 如果我不使用mysql_real_escape_string函数,则代码可以正常工作。 But the function is returning nothing! 但是函数什么也不返回! I read that the problem may be due to the fact that I do not have a mysql connection but that does not seem to be the case! 我读到该问题可能是由于我没有mysql连接,但事实并非如此!

Please help! 请帮忙!

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

$title = mysqli_real_escape_string($_POST["title"]);
$comment = mysqli_real_escape_string($_POST["comment"]);
$type = $_POST["type"];
$time = date("Y-m-d H:i:s");


$sql="INSERT INTO posts
VALUES
('','$type','$time','$time','$title','$comment','0','0','0','0','0')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }



mysqli_close($con);
header ("location: index.php");
?>

您需要将连接传递给函数

$title = mysqli_real_escape_string($con, $_POST["title"]);

According to http://php.net/manual/en/mysqli.real-escape-string.php you need to pass two parameters unless you are using the object oriented style.You should be using the format: 根据http://php.net/manual/en/mysqli.real-escape-string.php ,除非您使用面向对象的样式,否则您需要传递两个参数,应使用以下格式:

mysqli_real_escape_string ( $link , $escapestr ) mysqli_real_escape_string($ link,$ escapestr)

Where $link is: A link identifier returned by mysqli_connect() or mysqli_init() 其中$ link是:mysqli_connect()或mysqli_init()返回的链接标识符

And $escapestr is: The string to be escaped. $ escapestr是:要转义的字符串。 Characters encoded are NUL (ASCII 0), \\n, \\r, \\, ', ", and Control-Z. 编码的字符为NUL(ASCII 0),\\ n,\\ r,\\,',“和Control-Z。

php.net says php.net说
Procedural style 程序风格
mysqli_real_escape_string ( mysqli $link , string $escapestr )
So you will need to add your $con to it: 因此,您需要在其中添加$ con:
$title = mysqli_real_escape_string($con, $_POST["title"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM