gpt4 book ai didi

mysql - Drupal自定义表单获取并记录当前用户

转载 作者:行者123 更新时间:2023-11-29 12:03:52 24 4
gpt4 key购买 nike

我在互联网上查找并编写了一个自定义模块。

我想添加一个表单,供人们向数据库中添加一些信息。

我使用“data”模块创建一个名为“profile”的表。

我的模块的代码是(addfood.module):

    <?php


/**
* Implements hook_menu().
*/
function addfood_menu() {
$items['food/add'] = array(
'title' => '新增食物檔案',
'page callback' => 'addfood_page',
'access callback' => TRUE,
);
return $items;
}

/**
* Implements hook_permission.
*/
function addfood_permission() {
return array(
'addfood module' => array(
'title' => t('Addfood module permission'),
));
}

/**
* Returns form render array.
*/
function addfood_form($form, &$form_state) {
if (user_access('addfood module')) {
//Allowed

$form['name'] = array(
'#title' => t('名稱'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['calorie'] = array(
'#title' => t('卡路里'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('填寫卡路里(單位:千卡)'),
);
$form['water'] = array(
'#title' => t('水'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['protein'] = array(
'#title' => t('蛋白質'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['saturated_fat'] = array(
'#title' => t('飽和脂肪'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['trans_fat'] = array(
'#title' => t('反式脂肪'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['carbohydrates'] = array(
'#title' => t('碳水化合物'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['dietary_fiber'] = array(
'#title' => t('膳食纖維'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['cholesterol'] = array(
'#title' => t('膽固醇'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sodium'] = array(
'#title' => t('鈉'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sugars'] = array(
'#title' => t('糖'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['notes'] = array(
'#title' => t('Please explain your what makes you a prime candidate for our beta test'),
'#type' => 'textarea',
'#resizable' => TRUE,
'#description' => t('Beta spaces are limited, but let us know if there is a really good reason to let you in.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
} else {
//Access denied
header('Location: ../user/login?destination=food/add');
}
}

/**
* Menu callback.
*/
function addfood_page() {
return drupal_get_form('addfood_form');
}

/**
* Submission handler for form_example -> Insert into database
*/
function addfood_form_submit($form, &$form_state) {
$fe_id = db_insert('profile')
->fields(array(
'name' => $form_state['values']['name'],
'calorie' => $form_state['values']['calorie'],
'water' => $form_state['values']['water'],
'protein' => $form_state['values']['protein'],
'saturated_fat' => $form_state['values']['saturated_fat'],
'trans_fat' => $form_state['values']['trans_fat'],
'carbohydrates' => $form_state['values']['carbohydrates'],
'dietary_fiber' => $form_state['values']['dietary_fiber'],
'cholesterol' => $form_state['values']['cholesterol'],
'sodium' => $form_state['values']['sodium'],
'notes' => $form_state['values']['notes'],
))
->execute();

drupal_set_message(t('HO GYA Submit!!!!'));

return $form;
}
?>

如果我想记录谁自动添加信息(通过获取当前用户名)。

有人知道该怎么做吗?谢谢。

图是我的表架构。
(来源:libk.info)

最佳答案

您可以使用全局用户变量来获取当前用户。您可以测试一下:

<?php
global $user;
print_r($user->name);
?>

关于mysql - Drupal自定义表单获取并记录当前用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31904268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com