如何插入“复选框”值到 SQL

发布于 2024-12-18 03:28:55 字数 599 浏览 1 评论 0原文

好吧,我对如何将“复选框”值插入 SQL 感到困惑 这是我的代码

$form['last'] = array(
'#type' => 'checkboxes',
'#title' => "Just title",
'#options' => array(
'opt1' => "Option 1",
'opt2' => "Option 2",
),

,你可以看到我的表单由两个复选框组成,那么如何获取值并插入到 sql 语言中。任何人都可以在这里给出一个例子或提示

我用来获取值的方法(我知道这是非常错误的)

function fasil_form_submit($form,&$form_state){
  global $user;
  $entry = array(
  'uid'     => $user->uid,
  'test1'   => $form_state['values']['1first'],
  $jenis = 'aa_test';
  $return = insert_form($entry,$jenis);
}

ps:抱歉我的英语不好

well i'm confused on how to insert "Checkboxes" values to SQL
here's my code

$form['last'] = array(
'#type' => 'checkboxes',
'#title' => "Just title",
'#options' => array(
'opt1' => "Option 1",
'opt2' => "Option 2",
),

as you can see my form consist of two checkbox, so how to get the value and insert to sql language. anyone can give an example or hint

here the method that i'm used to get the value (i know its very wrong)

function fasil_form_submit($form,&$form_state){
  global $user;
  $entry = array(
  'uid'     => $user->uid,
  'test1'   => $form_state['values']['1first'],
  $jenis = 'aa_test';
  $return = insert_form($entry,$jenis);
}

ps : sorry for my bad english

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-25 03:28:55

我不是 100% 确定您要做什么,但我认为您正在尝试在数据库中为每个选中的复选框插入一个值?如果是这样,这是最快的方法:

function fasil_form_submit($form,&$form_state){
  // Filter out un-checked items
  $checked = array_filter($form_state['values']['last']);

  global $user;
  foreach ($checked as $value) {
    $entry = array(
      'uid' => $user->uid,
      'test1' => $value
    );
    $jenis = 'aa_test';
    insert_form($entry, $jenis);
  }
}

正如另一个答案中已经提到的,查看需要从表单中获取内容的最简单方法是在提交函数中输出 $form_state['values'] 来查看从表单传递的内容。

然而,不要使用难看的 print_r 并可能通过过早调用 exit 来搞乱表单提交(在 Drupal 7 中 drupal_exit() 应该始终是无论如何使用而不是 exit),我强烈建议您下载并安装 Devel module 并使用它的 dpm() 函数将变量打印到屏幕上。

传递给 dpm() 的任何变量都会输出到标准 Drupal 消息区域,并成为易于导航的该变量的屏幕层次结构,如下所示:

Output of dpm() function

您绝对可以在 Drupal 代码中的任何地方使用它,例如

function fasil_form_submit($form,&$form_state){
  // Output the form submission array to the messages area:
  dpm($form_state['values']);
}

Devel 模块非常好,对于任何严肃的 Drupal 开发来说绝对是必不可少的。

希望有帮助。

I'm not 100% sure what you're trying to do but I think you're trying to insert a value in the database for each of the checkboxes ticked? If so this is the quickest way:

function fasil_form_submit($form,&$form_state){
  // Filter out un-checked items
  $checked = array_filter($form_state['values']['last']);

  global $user;
  foreach ($checked as $value) {
    $entry = array(
      'uid' => $user->uid,
      'test1' => $value
    );
    $jenis = 'aa_test';
    insert_form($entry, $jenis);
  }
}

As already mentioned in another answer the simplest way to see what you need to get from the form is to output $form_state['values'] in your submission function to see what was passed from the form.

However, rather than use the unsightly print_r and potentially messing up the form submission by calling exit prematurely (in Drupal 7 drupal_exit() should always be used instead of exit anyway), I strongly recommend you download and install the Devel module and use it's dpm() function to print the variable to the screen.

Any variable passed to dpm() is outputted to the standard Drupal messages area, and becomes an easy to navigate on-screen hierarchy of that variable like this:

Output of dpm() function

You can use it absolutely anywhere in code within Drupal, e.g.

function fasil_form_submit($form,&$form_state){
  // Output the form submission array to the messages area:
  dpm($form_state['values']);
}

The Devel module is very good, and absolutely essential for any serious Drupal development.

Hope that helps.

人生百味 2024-12-25 03:28:55

这是我喜欢查看表单输出的快速方法

function fasil_form_submit($form,&$form_state){
  header('content-type: Text/plain');
  print_r($form_state['values']);
  exit;

  global $user;
  $entry = array(
  'uid'     => $user->uid,
  'test1'   => $form_state['values']['1first'],
  $jenis = 'aa_test';
  $return = insert_form($entry,$jenis);
}

现在这 3 行应该可以清楚地说明 Drupal 如何提交数据。

我相信您需要循环遍历 $form_state['values']['last'] 并测试该键是否未设置为 0。

function fasil_form_submit($form,&$form_state){
  global $user;
  foreach ($form_state['values']['last'] as $key => $value) {
    if (value != 0) {
      // $form_state['values']['last'][$key] was checked
    }
    else {
      // $form_state['values']['last'][$key] not checked
    }
  }
}

Here's a quick way I like to look at my form output

function fasil_form_submit($form,&$form_state){
  header('content-type: Text/plain');
  print_r($form_state['values']);
  exit;

  global $user;
  $entry = array(
  'uid'     => $user->uid,
  'test1'   => $form_state['values']['1first'],
  $jenis = 'aa_test';
  $return = insert_form($entry,$jenis);
}

Now those 3 lines should make it clear how Drupal is submitting the data.

I believe you will need to loop through $form_state['values']['last'] and test that the key is not set to 0.

function fasil_form_submit($form,&$form_state){
  global $user;
  foreach ($form_state['values']['last'] as $key => $value) {
    if (value != 0) {
      // $form_state['values']['last'][$key] was checked
    }
    else {
      // $form_state['values']['last'][$key] not checked
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文