2013-08-01 52 views
0

我有下面的SQL語句..MYSQL聲明不正確呼應

$sql = "select tableid, comment, ID, description from reservations 
     where uniqueid = '" . $reservationid . "'"; 

$ ReservationId爲通過POST形式通過。選擇不同字段的非常類似的聲明似乎在另一頁上正常工作。但是,當我回聲頁面上的SQL語句,我得到

select tableid, comment, ID, description 
from reservations 
where uniqueid = '51e5f949c1828' 

這似乎跳過最後的單引號

我也試過

$sql = "select tableid, comment, ID, description from reservations 
     where uniqueid = '$reservationid'"; 

具有相同的結果。如果我查看網頁源代碼,我不明白爲什麼我看到這個..

select tableid, comment, ID, description from reservations 
where uniqueid = '51e5f949c1828</form 

在地球上是流浪</form>部分來自哪裏?

+0

你可以發佈示例代碼 – 2013-08-01 10:59:04

+0

您應該使用*預處理語句*畢竟。 –

+1

發佈您的代碼。在sql中沒有錯誤。您可能還沒有以 –

回答

1

但是,當我回聲頁面上的SQL語句,我得到

select tableid, comment, ID, description from reservations where uniqueid = <'51e5f949c1828 

如果你「回聲」到你的瀏覽器窗口,有很大的機會,該瀏覽器的去掉</form>從顯示器。我的下注是如果您在顯示查詢時查看頁面的源代碼,您也會看到... where uniqueid = '51e5f949c1828</form>

正如你肯定$reservationid包含你的期望?

你能不能展示一些代碼。像你的HTML表單?在那裏可能存在格式錯誤的HTML和/或未封閉的引用。


BTW,你永遠不應該直接從您的查詢不受信任的源注入的變量。這導致SQL injection

+0

您是對的!當我回復變量時,瀏覽器刪除了一個流浪標籤。我將採取措施解決sql注入問題。 – user2522177

0

使用sprintf() - 這是像組合字符串這種類型的更好的方法。

0

我認爲你有你的單引號和雙引號混合起來。

$sql = "select tableid, comment, ID, description from reservations where uniqueid = "' . $reservationid . '""; 
1

看來你的表格中有一些錯誤。發送的數據不正確完成,因此HTML格式的一部分與它一起發送。如果沒有發佈您發送數據的方式,就無法分辨出來。 您必須糾正此錯誤才能使代碼正常工作。

有什麼不對您提交的PHP,但它是SQL注入攻擊一個容易的目標等,所以最好去使用,例如sprintf的。 http://php.net/manual/en/function.sprintf.php

0

你也可以做您的查詢像

$sql = "select tableid, comment, ID, description 
     from reservations where uniqueid = '$reservationid'"; 

或者乾脆

$sql = "select tableid, comment, ID, description 
     from reservations where uniqueid = $reservationid ";