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

Drupal 7 使用Drupal 7中的自定义表单模块编辑并更新数据库,drupal-7,Drupal 7,我正试图通过单击表中的任何记录来编辑/删除它们。该表位于自定义表单模块中。为了得到帮助,我想出了这个。在那里,他们通过用户的角色来控制权限。在我的情况下,我希望它对每个人都可用。我是Drupal的新手,因此,我无法在这方面获得任何重大的运气。我要删除任何记录,但编辑仍然没有运气。我检查了查询,没有默认的字段,它的工作。但是只有使用默认值时,我才想显示表单,以便以后编辑。所以到现在为止,这就是我所编码的 <code> <?php function products_menu(){

我正试图通过单击表中的任何记录来编辑/删除它们。该表位于自定义表单模块中。为了得到帮助,我想出了这个。在那里,他们通过用户的角色来控制权限。在我的情况下,我希望它对每个人都可用。我是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>

有什么建议我做错了什么吗?

我已经找到了解决方案

而不是这个

$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,
            );
现在工作:

要了解更多属性,请访问此网站