【发布时间】:2015-06-02 22:03:28
【问题描述】:
更新问题:
我已使用此代码成功地将一些 html 添加到我的 MySQL 数据库中:
$emailaddress = $_SESSION["email_address"];
..
$html_content = '<div> Hello</div>';
..
$stmt = $db->prepare("INSERT INTO users(htmlcontent) VALUES (:hContent)");
$stmt->execute(array(':hContent' => $html_content));
除了我需要它只插入到与$emailaddress. 匹配的电子邮件的“htmlcontent”列中,所以我尝试在我的 PDO 语句中包含WHERE,如下所示:
$stmt = $db->prepare("INSERT INTO users(htmlcontent) VALUES (:hContent) WHERE email = :email");
$stmt->execute(array(':hContent' => $html_content, ':email' => $emailaddress));
但它返回以下错误:也许我应该改用更新?我现在试试
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE email = 'my@domain.com'' at line 1'
in /home/fire18/public_html/login/private.php:31 Stack trace: #0 /home/fire18/public_html/login/private.php(31): PDOStatement->execute(Array) #1 {main} thrown in /home/firer18/public_html/login/private.php on line 31
老问题:
我正在尝试将纯 html 代码插入到我的 MySQL 数据库中,以便稍后从不同的页面检索它。我很困惑如何将 html 放入数据库,然后再检索它。我读过我应该使用TEXT 和mysql_real_escape_string() 为INSERT 准备html。但是我正在使用 PDO,所以我不知道该怎么做。
所以,假设我想将此 html 放入我的数据库中:
<li>
City Scene
<input id="keepbox5" type="checkbox" name="keepbox5" />
<label for="keepbox5">Keep</label>
<input id="cbox5" type="checkbox" name="cbox5" class="cboxes"/>
<label for="cbox5">Show</label>
<div class="tinybox">
<img src="http://www.example.com/img/temp.jpg" alt="tinypic" id="tinypic" style="display:none;">
</div>
</li>
我需要将INSERT 它添加到我的users 表中,但我确定这是错误的:
$htmlCode = mysql_real_escape_string(<li> City Scene <input id="keepbox5" ...[ALL HTML]... </li>);
$query = "
INSERT INTO users (
db_html_code
) VALUES (
$htmlCode
)
";
因为我像这样使用 PDO:
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
最后,当我需要检索数据时,我读到我需要使用stripslashes(),但同样,不知道这是否适用。
- 如何正确地将纯 html 插入我的数据库(使用 PHP 的 PDO?
- 我如何在其他页面上请求/显示存储的 html?
感谢您的反馈
【问题讨论】:
-
插入任何字符串的方式相同
-
那么我上面的例子正确吗?
$htmlCode = mysql_real_escape_string(<li> City Scene <input id="keepbox5" ...[ALL HTML]... </li>);我猜不是因为 mysql 显然已被弃用,但我不知道它应该是什么样子。 -
没有无效的 php 语法
-
你愿意告诉我它应该是什么样子吗?我仍然不明白如何检索数据。如果你不愿意,那好吧。
-
您不能混合使用 MySQL API。与 PDO 联系并与 PDO 保持联系。不要使用
mysql_之类的其他功能,它不起作用。这里php.net/mysqlinfo.api.choosing