Php 在mysql中插入双引号并在不使用反斜杠的情况下打印出来
我有一个表格,其中必须插入以下类型的代码:Php 在mysql中插入双引号并在不使用反斜杠的情况下打印出来,php,mysql,echo,backslash,mysql-real-escape-string,Php,Mysql,Echo,Backslash,Mysql Real Escape String,我有一个表格,其中必须插入以下类型的代码: <iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d3035.058400512634!2d-3.6438669999999997!3d40.473973!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0xd422eea55d33c51%3A0x3294408e8e67eff4!2sClinica+Dental+Artidental!5e0!3m
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d3035.058400512634!2d-3.6438669999999997!3d40.473973!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0xd422eea55d33c51%3A0x3294408e8e67eff4!2sClinica+Dental+Artidental!5e0!3m2!1ses!2ses!4v1419779677798" width="600" height="450" frameborder="0" style="border:0"></iframe>
而iframe的插入方式如下所示:
$mapLink = htmlspecialchars($_POST['mapLink'], ENT_QUOTES);
<iframe src=\"https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d3035.058400512634!2d-3.6438669999999997!3d40.473973!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0xd422eea55d33c51%3A0x3294408e8e67eff4!2sClinica+Dental+Artidental!5e0!3m2!1ses!2ses!4v1419779677798\" width=\"600\" height=\"450\" frameborder=\"0\" style=\"border:0\"></iframe>
如何才能取回我的初始链接
“将字符串分配给$mapLink
时将其转义:
$mapLink=htmlspecialchars($\u POST['mapLink'],ENT\u引号);
如果要按原样将其插入数据库,只需从post中获取值,而不要对其进行转义
要防止SQL注入,请使用mysql绑定,如下所示:
$stmt=$mysqli->prepare(“插入到sometable(fieldA、mapField、fieldC)值(?,?)”;
$stmt->bind_param('sss',$someVar,$mapLink,$otherVar);
请参阅有关参数绑定的更多信息
如果您在使用魔法引号时遇到问题,您可以这样剥离它们:
$mapLink=stripslashes($_POST['mapLink']);
你试过了吗
如果需要原始值,为什么首先要对其进行转义?htmlspecialchars
不是用于数据库引用的。您需要理解转义的实际含义。然后需要禁用魔引号。@mario我进行转义,因为否则双引号会弄乱我的PHP,并停止插入截断数据整体价值我们已经得出结论,你在其他地方做错了什么。现在你正在寻找错误的解决方法。当我直接插入数据库时,它在双引号前出现反斜杠…我正在准备并执行一个array@samyb8请发布用于将其插入数据库的代码。如果从$\u POST['mapLink']
绑定一个值,不做任何修改,反斜杠不应该出现。@samyb8我不擅长PDO(我主要使用mysqli),但你能变量转储($mapLink)
在插入前查看字符串是否被转义或在插入时发生?@samyb8另外,如果您使用的是PHP 5.4或更早版本,则可能是“魔术引号”造成的。我已将如何删除它们添加到我的答案中。魔术引号是问题所在。谢谢!
$editCentro = $con->prepare("UPDATE centros SET active = :active, nombre = :nombre, zona = :zona, address = :address,
metro = :metro, zip = :zip, phone = :phone, fax = :fax, email = :email, mapLink = :mapLink, descripcion = :descripcion, horarios = :horarios
WHERE id = ".$centroId);
$editCentro->execute(array(':active'=>$active, ':nombre'=> $nombre, ':zona'=>$zona, ':address'=>$address, ':metro'=>$metro,
':zip'=>$zip, ':phone'=>$telefono, ':fax'=>$fax, ':email'=>$email, ':mapLink'=>$mapLink, ':descripcion'=>$descripcion, ':horarios'=>$horarios));
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
?>