2013-03-17 40 views
-3

我有一個$ _post值,我需要把它放在一個PHP查詢中。已經嘗試了很多不同的方式,但無法實現它的工作。

想法?

$result = $xpath->query('/stickers/sticker[id=2]/text'); 

我需要改變[id=2]這個$_POST['textBoxID']

+0

[字符串](http://php.net/string)和[字符串運算符](http://www.php.net/manual/en/language.operators.string.php) – 2013-03-17 20:52:19

+0

看看http://php.net/operators.string並解決了這個問題。請在SO上詢問之前進行一些搜索。在[ask] – 2013-03-17 20:53:25

回答

1

$_POST['textBoxID']只是數值?如果是的話,你可以這樣做:

$result = $xpath->query('/stickers/sticker[id='.$_POST['textBoxID'].']/text'); 

或者,如果$_POST['textBoxID']將是整個ID = X做到這一點:

$result = $xpath->query('/stickers/sticker['.$_POST['textBoxID'].']/text'); 

在這裏閱讀更多:http://php.net/operators.string

+0

這是一個數字值。像魅力一樣工作......錯過了點。 非常感謝。讚賞 – Dymond 2013-03-17 20:57:37

+1

非常歡迎:) – user1909426 2013-03-17 22:09:18

1

您需要用雙引號或級聯。單引號導致您的變量被視爲文字。雙引號將允許它們插補。

$result = $xpath->query("/stickers/sticker[$_POST[textBoxID]]/text"); 

$result = $xpath->query("/stickers/sticker[{$_POST['textBoxID']}]/text"); 

$result = $xpath->query('/stickers/sticker['.$_POST['textBoxID'].']/text'); 
+0

或者使用'query('/ strickers/sticker [$ _POST [textBoxID]]/text');' – 2013-03-17 20:54:15

1

你想無論是

$thePost = $_POST['textBoxID']; 
$result = $xpath->query('/stickers/sticker'.$thePost.'/text'); 

$thePost = $_POST['textBoxID']; 
$result = $xpath->query('/stickers/sticker[id='.$thePost.']/text');