简体   繁体   English

如何使用 php 将 HTML 代码插入到数据库中

[英]how to insert HTML code into DB using php

i want to update my database with new html code this it the query:我想用新的 html 代码更新我的数据库,这是查询:

UPDATE `Pages` SET `content`= '<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p>&nbsp;</p>
</div>
</div>
</div>' 

but all the time i get an error.但我总是遇到错误。 how can i update my database, i don't know what will be inside this html code, is there a function that make all the code like string without special sign?我如何更新我的数据库,我不知道这个 html 代码里面会有什么,是否有一个函数可以让所有的代码像没有特殊符号的字符串一样?

EDIT:: the problem is with the special char like ' i can't change the html code, is user chice to put it.编辑:: 问题出在特殊字符上,例如 ' 我无法更改 html 代码,用户是否可以放置它。

Do following using addslashes() function, so it will help easily to insert update html to使用addslashes()函数执行以下操作,以便轻松插入更新 html 到

UPDATE `Pages` SET `content`= addslashes('<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p>&nbsp;</p>
</div>
</div>
</div>') 

Try this:- $htmlcode = mysql_real_escape_string($htmlcode);试试这个:- $htmlcode = mysql_real_escape_string($htmlcode);

For example:-例如:-

$htmlcode = '<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p>&nbsp;</p>
</div>
</div>
</div>';

$htmlcode = mysql_real_escape_string($htmlcode);

UPDATE `Pages` SET `content`= '$htmlcode';

store your html content in one variable and use addslashes() when you are inserting it to database.将您的 html 内容存储在一个变量中,并在将其插入数据库时​​使用 addlashes()。

$content='<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p>&nbsp;</p>
</div>
</div>
</div>';

and write your query as below并写下您的查询如下

UPDATE `Pages` SET `content`=addslashes($content);

Hope this will help you :)希望能帮到你 :)

I assume this will do the trick我认为这会解决问题

Encode your text编码您的文本

when you get the text back from the database just decode it back当您从数据库中取回文本时,只需将其解码回来

This could be duplicate of this post这可能是这篇文章的重复

in any case... this could be the solution for you无论如何......这可能是你的解决方案

$html = mysql_real_escape_string($html);
$sql = "UPDATE `Pages` SET `content`= $html";

I think there should be a (') single Quote into your string.我认为您的字符串中应该有一个 (') 单引号。

You can use the 'htmlspecialchars' function with ENT_QUOTES as second argument.您可以使用带有 ENT_QUOTES 作为第二个参数的 'htmlspecialchars' 函数。

And also 'mysql_real_escape_string' function can be used.也可以使用 'mysql_real_escape_string' 函数。

Like喜欢

$hcode = '<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p>&nbsp;</p>
</div>
</div>
</div>';
$hcode = htmlspecialchars($hcode, ENT_QUOTES);
UPDATE `Pages` SET `content`= '$hcode';

Would be nice if you could use PDO to prepare the statement before and then you would insert the data.如果您可以在之前使用 PDO 准备语句然后插入数据,那就太好了。 The variables you would insert can be anything, you do not need to care if they have ' or " or ' " > It is all fine, by using the prepare() we are saying that we will insert the following variables without any change.你要插入的变量可以是任何东西,你不需要关心它们是否有 ' 或 " 或 ' " > 一切都很好,通过使用 prepare() 我们是说我们将插入以下变量而不做任何更改。 So even if you have code or sql injection it will consider as text no matter what.因此,即使您有代码或 sql 注入,无论如何它都会被视为文本。 You could do a PDO connection like this:您可以像这样进行 PDO 连接:

$host = 'hostname';
$user = 'duh_user';
$password = 'duh_pwd';
$dbname = 'myDatabase';

$dsn = 'mysql:host='.$host.';dbname='.$dbname.';charset=utf8mb4';
$pdo = new PDO($dsn, $user, $password);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);


$column1 = $htmlcode;
$column2 = $anything;
$column3 = $reallyAnything;

$query = "INSERT INTO table_name(column1, column2, column3) VALUES (?, ?, ?)";
$stmt = $pdo->prepare($query);
$stmt->execute([$column1, $column2, $column3]);

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

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