使用Drupal 7中的自定义表单模块编辑然后更新数据库

时间:2015-03-31 12:07:23

标签: drupal-7

我正在尝试编辑/删除表中的任何记录,点击它们。该表位于自定义表单模块中。为了获得帮助,我提出了LINK。他们在那里控制着用户角色的权利(我猜)。就我而言,我希望每个人都可以使用它。我是Drupal的新手,所以我无法获得任何重大的运气。我要删除任何记录,但编辑仍然没有运气。我检查查询没有默认字段,它的工作。但是只使用默认值我想显示表单,以便以后编辑。所以到目前为止,这就是我编码的内容。

<code>
<?php
function products_menu(){
 $items = array();
     $items['products_form'] = array(
        'title' => t('Products'),
        'page callback' => 'products_list',
        'access callback' => TRUE,
    );
    $items['products_form/delete/%'] = array(
        'title' => t('Delete Product'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('products_delete_confirm', 2),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK, 
    );
    $items['products_form/edit/%'] = array(
        'title' => t('Edit Product'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('products_edit_confirm', 2),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );

 return $items;
}


function products_list() {
    $header = array('CODE','NAME','OBJECT TYPE',' ');
    $results = db_query("SELECT code,name,object_type FROM {products}");
    $rows = array();
    foreach($results as $key) {
        $code = $key->code;
        $name = $key->name;
        $object_type = $key->object_type;
      $rows[] = array($code,$name,$object_type,"<a href='products_form/edit/{$key->code}'>" . t('Edit') . "</a> | <a href='products_form/delete/{$key->code}'>" . t('Delete') . "</a>");
    }
    return theme('table', array('header' => $header, 'rows' => $rows));
}


function products_delete_confirm($form ,&$form_state, $product_code) {
$form['_product'] = array(
        '#type' => 'value',
        '#value' => $product_code,);

        drupal_set_message($product_code);

return confirm_form($form,t('Are you sure you want to delete '.$product_code.' Product?'),
        isset($_GET['destination']) ? $_GET['destination'] : "products_form",t('This action cannot be undone.'),t('Delete'),t('Cancel'));

}


function products_delete_confirm_submit($form, &$form_state) {
    $form_values = $form_state['values'];
 if ($form_state['values']['confirm']) {
$products = $form_state['values']['_product'];
               drupal_set_message(t('Product ' .$products.' will get deleted.'));

                    $result = db_query("DELETE FROM {products} where code='{$products}'");
 drupal_set_message(t('Products has been deleted successfully.'));}
 drupal_goto("products_form");
}
function products_edit_confirm($form ,&$form_state, $product_code){
drupal_set_message($product_code);
$code = '';$name='';$object_type='';
$results = db_query("SELECT code,name,object_type FROM {products} WHERE code='{$product_code}'");
    foreach($results as $key) {
        $code = $key->code;
        $name = $key->name;
        $object_type = $key->object_type;
    }
$form = array();

     $form['code']=array(
            '#title'=>t('CODE'),
            '#type'=>'textfield',
            '#value' => $code,);
    $form['name']=array(
            '#title'=>t('NAME'),
            '#type'=>'textfield',
            '#value' => $name,);
    $form['object_type']=array(
            '#title'=>t('OBJECT TYPE'),
            '#type'=>'textfield',
            '#value' => $object_type,);

    return confirm_form($form,t(''),
            isset($_GET['destination']) ? $_GET['destination'] : "products_form",
            t(''),t('Edit'),t('Cancel'));
}


function products_edit_confirm_submit($form, &$form_state)
{
$form_values = $form_state['values'];
if ($form_state['values']['confirm']) {

    $code = $form_state['values']['code'];
    $name = $form_state['values']['name'];
    $object_type= $form_state['values']['object_type'];

    $rs = db_query("UPDATE  {products} SET name= '$name', object_type = '$object_type' WHERE  code='{$code}'");
     drupal_set_message(t('Products has been updated successfully.'));
     }
    drupal_goto("products_form");
}


</code>

有什么建议我做错了吗?

1 个答案:

答案 0 :(得分:0)

我已经有了解决方案......

而不是这个

$form['code']=array(
            '#title'=>t('CODE'),
            '#type'=>'textfield',
            '#value' => $code,);
    $form['name']=array(
            '#title'=>t('NAME'),
            '#type'=>'textfield',
            '#value' => $name,);

我试过了......

$form['code']=array(
                '#title'=>t('CODE'),
                '#type'=>'textfield',
                '#default_value' => $code,
            );
    $form['name']=array(
                '#title'=>t('NAME'),
                '#type'=>'textfield',
                '#default_value' => $name,
            );

现在它的工作:)

要了解更多资源,请访问此网站https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7