gpt4 book ai didi

php - 使用 PHP 查询在 MySQL 中插入 HTML 文件

转载 作者:行者123 更新时间:2023-11-28 23:33:11 26 4
gpt4 key购买 nike

我有一个像这样的 heredoc 变量:

$status=<<<EOT
<p>hello world</p>
<p>I'm <strong>really</strong>OK!</p>
<p></p>
EOT;

我想像这样通过 PHP 将它插入到我的带有空格的 MySQL 数据库中:

query="INSERT INTO requests (ID,title) VALUES ('$ID','$status')";

可是我做不到。我应该怎么做才能插入它?

最佳答案

你可以通过两种方式做到这一点:

像这样使用 mysqli_real_escape_string():

$mydb = new mysqli("localhost","root","FedAnd11");

$status=<<<EOT
<p>hello world</p>
<p>I'm <strong>really</strong>OK!</p>
<p></p>
EOT;

$query="INSERT INTO requests (ID,title) VALUES ('$ID','".$mydb->real_escape_string($status)."')";

或者如果您还没有数据库连接,

$status=<<<EOT
<p>hello world</p>
<p>I'm <strong>really</strong>OK!</p>
<p></p>
EOT;

$status = str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $status);

$query="INSERT INTO requests (ID,title) VALUES ('$ID','$status')";

如果我理解你的问题。

你可以做的另一件事是使用 mysql 准备语句,如果你真的想按原样放置 $status,如下所示:

$status=<<<EOT
<p>hello world</p>
<p>I'm <strong>really</strong>OK!</p>
<p></p>
EOT;

$stmt = $dbConnection->prepare('INSERT INTO requests (ID,title) VALUES (?,?)');
$stmt->bind_param('is', $ID,$status);

$stmt->execute();

我假设 $ID 是整数。

关于php - 使用 PHP 查询在 MySQL 中插入 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36720198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com