查詢要在條件中使用,當空間介於值之間時

[英]Query to be used in the conditions, when space in between the value


I am using JQuery and CakePHP for my application. I have a table Attributes(id,form_id,label,type)

我正在為我的應用程序使用JQuery和CakePHP。我有一個表Attributes(id,form_id,label,type)

In this table, I have an entry like

在這張表中,我有一個條目

1 1 First name Text
2 1 Lastname Text

I have written a CakePhp query to retrieve the id given the form_id and the label

我編寫了一個CakePhp查詢來檢索給定form_id和標簽的id

$attri = $this->Attribute->find('all', array(
   'fields'=> array('Attribute.id'),
   'conditions' => array(
      'Attribute.form_id' => $id,
      'Attribute.label' => $key
   )
));

where $key is my label and $id is my form_id. However, since the First name includes the space in between, the conditions didn't work well, but it works for Lastname since it didn't have space in between. How can I fix this?

其中$ key是我的標簽,$ id是我的form_id。但是,由於名字包含介於兩者之間的空格,因此條件不能很好地工作,但它適用於Lastname,因為它之間沒有空格。我怎樣才能解決這個問題?

Edit:

I am using MySQL database. The user can keep whatever the fieldname. I got the parameters $key from a form using $_POST, e.g.:

我正在使用MySQL數據庫。用戶可以保留任何字段名稱。我使用$ _POST從表單中獲取參數$ key,例如:

foreach ($_POST as $key => $value): 
echo $key; //displays First name correctly. 

But when I use $key in the conditions, only if no spaces are in between the label it accepts, it will return the attribute id, else nothing is returned.

但是當我在條件中使用$ key時,只有當它接受的標簽之間沒有空格時,它才會返回屬性id,否則不返回任何內容。

I even tried with using "$key", but it doesn't work either.

我甚至試過使用“$ key”,但它也不起作用。

1 个解决方案

#1


The bug must be somewhere else in your code. It is perfectly fine to have spaces in the values that you use for conditions. See this page in the CakePHP book for an example. CakePHP automatically escapes the values for you so they work, even with spaces.

該錯誤必須位於代碼中的其他位置。在用於條件的值中包含空格是完全正常的。有關示例,請參閱CakePHP手冊中的此頁面。 CakePHP會自動為您轉義值,以便它們可以工作,即使是空格。

Have a look at your Attribute model and see what happens in the beforeFind or afterFind callbacks.

查看您的屬性模型,看看beforeFind或afterFind回調中發生了什么。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/06/7211581d8f67861fd1d11e8002254fb8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com