2013-05-09 87 views
-1

我試圖在數據庫中插入段落(包含中間單引號),如下所示。插入段落時發生Mysql錯誤

$rule="**Mobile's** are not allowed into the room...................soon"; 


mysql_query("insert into table_name (rule) values('$rule')"); 

在執行該段不插入查詢。我直接在Mysql中使用SQL選項嘗試。那裏顯示錯誤。

任何建議..?

感謝

+0

是「錯誤」可能是有趣的閱讀,你不覺得嗎?另外,用那個,我已經知道,你可以很容易地調試你的問題 – 2013-05-09 12:58:10

+0

我建議你告訴我們錯誤 – Dima 2013-05-09 12:58:35

回答

0

使用

$rule = mysql_real_escape_string("**Mobile's** are not allowed into the room...................soon"); 
+0

Thnku它的工作完美..! – 2013-05-09 13:05:41

-1

嘗試用: 的mysql_query( 「插入到表名SET規則= '$規則'」);

+0

錯誤,這不會解決問題。 – 2013-05-09 13:01:00

1

一些問題,讓你覺得:

  1. 什麼雙引號(")意味着PHP?
  2. 它們對包含變量的字符串有什麼影響?
  3. 什麼是「輸入衛生」?
  4. 什麼是「字符轉義」?
  5. 什麼是PDO?

另外,也是最重要的,

Please, don't use mysql_* functions in new code。他們不再維護and are officially deprecated。請參閱red box?請改爲了解prepared statements,並使用PDOMySQLi - this article將幫助您決定哪個。如果您選擇PDO,here is a good tutorial

0

在將值發送到Mysql數據庫表之前,可以使用mysql_real_escape_string()。 請檢查此鏈接http://php.net/manual/en/function.mysql-real-escape-string.php

$rule="**Mobile's** are not allowed into the room...................soon"; 
$rule1=mysql_real_escape_string($rule) 
mysql_query("insert into table_name (rule) values('$rule1')"); 
1

來處理這樣的事情最好的辦法是使用PDO分機或MySQLi。它們旨在防止從SQL Injection

使用示例PDO

$rule = "**Mobile's** are not allowed into the room...................soon"; 
$stmt = $pdo->prepare("insert into table_name (rule) values(:rule)"); 
$stmt->execute(array(':rule' => $rule)); 
,我可以給解釋是多麼糟糕的查詢斷裂時的值包含單引號

最佳鏈接: