2014-12-28 56 views
0

我情況下,下列類型的代碼必須插入一個表格:插入雙引號到MySQL,並打印出來沒有反斜槓

<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 = htmlspecialchars($_POST['mapLink'], ENT_QUOTES); 

和iframe,插入如下:

&lt;iframe src=\&quot;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\&quot; width=\&quot;600\&quot; height=\&quot;450\&quot; frameborder=\&quot;0\&quot; style=\&quot;border:0\&quot;&gt;&lt;/iframe&gt; 

我怎樣才能以接回我怎麼做nitial鏈接"<iframe src..."用php打印出來,因爲它是最初寫的?如果沒有反斜槓,& QUOT等

更新1

這是我如何插入/更新的MYSQL:

  $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)); 

而且即使不逃逸的價值,它被插了在雙引號之前加上反斜槓...

+0

如果您需要原始值,您爲什麼首先將它轉義出來? 'htmlspecialchars'不適用於數據庫引用。 – mario

+0

您需要了解實際轉義的含義。然後你需要禁用魔術引號。 – SLaks

+0

@mario我逃跑,因爲否則雙引號與我的PHP搞砸了,並停止插入截斷整個值 – samyb8

回答

1

將字符串分配給$mapLink時轉義字符串:

$mapLink = htmlspecialchars($_POST['mapLink'], ENT_QUOTES); 

如果你想將它插入到數據庫中,不被簡單地從後採取的價值,逃避它。 (同樣,這不是逃逸你會用它來防止SQLI)

防止SQL注入,用mysql像這樣綁定:

$stmt = $mysqli->prepare("INSERT INTO sometable (fieldA, mapField, fieldC) VALUES (?, ?, ?)"); 
$stmt->bind_param('sss', $someVar, $mapLink, $otherVar); 

查看參數的詳細信息結合in the PHP docs here

如果你有魔術引號的問題,你可以帶他們像這樣:

$mapLink = stripslashes($_POST['mapLink']); 
+0

當我直接插入到數據庫中時,它在雙引號之前出現反斜槓....並且正在準備並使用數組執行 – samyb8

+0

@ samyb8請將用於將其插入數據庫的代碼發佈出去。如果你從'$ _POST ['mapLink']'綁定了一個沒有修改的值,反斜槓就不應該在那裏。 – Sebi

+0

當然,請參閱更新1 – samyb8

0

你試過html_entity_decode

<?php 
$orig = "I'll \"walk\" the <b>dog</b> now"; 

$a = htmlentities($orig); 

$b = html_entity_decode($a); 

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now 

echo $b; // I'll "walk" the <b>dog</b> now 
?>