我有一個$ _post值,我需要把它放在一個PHP查詢中。已經嘗試了很多不同的方式,但無法實現它的工作。
想法?
$result = $xpath->query('/stickers/sticker[id=2]/text');
我需要改變[id=2]
這個$_POST['textBoxID']
我有一個$ _post值,我需要把它放在一個PHP查詢中。已經嘗試了很多不同的方式,但無法實現它的工作。
想法?
$result = $xpath->query('/stickers/sticker[id=2]/text');
我需要改變[id=2]
這個$_POST['textBoxID']
將$_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
這是一個數字值。像魅力一樣工作......錯過了點。 非常感謝。讚賞 – Dymond 2013-03-17 20:57:37
非常歡迎:) – user1909426 2013-03-17 22:09:18
您需要用雙引號或級聯。單引號導致您的變量被視爲文字。雙引號將允許它們插補。
$result = $xpath->query("/stickers/sticker[$_POST[textBoxID]]/text");
或
$result = $xpath->query("/stickers/sticker[{$_POST['textBoxID']}]/text");
或
$result = $xpath->query('/stickers/sticker['.$_POST['textBoxID'].']/text');
或者使用'query('/ strickers/sticker [$ _POST [textBoxID]]/text');' – 2013-03-17 20:54:15
你想無論是
$thePost = $_POST['textBoxID'];
$result = $xpath->query('/stickers/sticker'.$thePost.'/text');
或
$thePost = $_POST['textBoxID'];
$result = $xpath->query('/stickers/sticker[id='.$thePost.']/text');
[字符串](http://php.net/string)和[字符串運算符](http://www.php.net/manual/en/language.operators.string.php) – 2013-03-17 20:52:19
看看http://php.net/operators.string並解決了這個問題。請在SO上詢問之前進行一些搜索。在[ask] – 2013-03-17 20:53:25