如何在drupal中显示表单并获取通配符变量

时间:2012-04-02 14:57:51

标签: drupal drupal-6 drupal-modules drupal-fapi drupal-forms

我有一个这样的菜单项:

$items['property/edit/%'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('property_edit_view',2),
    'access arguments' => array('access property edit page'),

    'type' => MENU_CALLBACK,
    );

现在我想要编辑旁边的ID,'%'? 这是处理它的功能。

function property_edit_view($id){
drupal_set_title("Edit the Property");
global $user;
$form = array();
dpm($id);
$sql = "SELECT * FROM {property} WHERE property_id= $id AND property_uid = $user->uid";

$result = db_query($sql);
$row = db_fetch_object($result);
$form['p_name'] = array(
'#type' => 'textfield',
'#title'=>t('Name of the Property'),
'#required'=>TRUE,
'#default_value'=>t($row->property_name),
);

return $form;
}

但我现在收到错误:

user warning: Unknown column 'Array' in 'where clause' query: SELECT * FROM property WHERE property_id= Array AND property_uid = 1 in C:\wamp\www\getting-in.com\sites\all\modules\property\property.module on line 284.

有什么方法可以解决它吗?

2 个答案:

答案 0 :(得分:1)

您需要使用%d而不是直接在SQL语句中使用变量。这是为了防止SQL注入,执行验证,并尝试允许您使用不同类型的数据库。然后在db_query函数的下一个参数中提供变量。 “占位符是”

  • %s,字符串
  • %d,int
  • %b,二进制
  • %%,实际上是一个“百分号”标志
  • %f,float

答案 1 :(得分:1)

表单构建器的函数签名应包含& $ form_state作为Drupal 6的第一个参数.drupal_get_form的任何其他参数将在$ form_state之后作为附加参数传递给表单构建器。例如:

function property_edit_view(&$form_state, $id) {
}

此外,您的代码包含Jonathan Rowny指出的SQL注入错误。