Drupal 7表单插入如果已存在则显示已存在的消息

时间:2016-03-02 07:05:38

标签: mysql drupal

function countries_form($form, &$form_state,$id=0) {

if($id!=0){
 $result =  db_query('SELECT * FROM {countries} WHERE id = '.$id.'')->fetch();
// print_r($result);exit;

 $form['country_name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'Country name',
    '#size' => 30,
    '#maxlength' => 30,
    '#default_value' => $result->country_name,
    '#required' => TRUE, //make this field required 
  );
  $form['description'] = array(
    '#type' => 'textarea', //you can find a list of available types in the form api
    '#title' => 'Description',
    '#default_value' => $result->description,
    '#required' => TRUE, //make this field required 
  );
  $form['status'] = array(
  '#type' => 'radios',
  '#title' => t('Status'),
  '#default_value' => $result->status,
  '#options' => array(
    '1' => t('Active'),
    '0' => t('Inactive'),

  ),
  );

  }else{
  $form['country_name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'Country name',
    '#size' => 30,
    '#maxlength' => 30,
    '#required' => TRUE, //make this field required 
  );
 $form['description'] = array(
    '#type' => 'textarea', //you can find a list of available types in the form api
    '#title' => 'Description',
    '#required' => TRUE, //make this field required 
  );
  }
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Click Here!'),
  );

  return $form;
}

function countries_form_submit($form, &$form_state) {
//$result =  db_query('SELECT country_name FROM {countries}')->fetch(); 
//print_r($result);
if(arg(2)!=0){

  $query = db_update('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description'],'status'=>$form_state['values']['status']))->condition('id',arg(2));

  $query->execute();
   // print_r($query); 
  drupal_set_message(t('Country %name has been updated.', array('%name' => $form_state['values']['country_name'])));

  //print_r($description);
  }else{
  //$query = db_update('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description'],'status'=>$form_state['values']['status']))->condition('country_name','%s');
 // print_r($query);

  $query = db_insert('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description']));
 //print_r($query);
  $query->execute(); 

  drupal_set_message(t('Country %name has been saved.', array('%name' => $form_state['values']['country_name'])));
  }

 //exit;
   $form_state['redirect'] = 'mypages/table';
  }

使用druapl 7表格我插入并显示日期,编辑和删除以及所有工作正常.. 现在我想..当插入表格我们需要显示消息,如果已经提交存在国家名称已经采取像那样..

没有更新查询只需要查询插入值检查天气它是否存在...

通过传递参数来使用或编辑以下代码。

if(arg(2)!=0){

  $query = db_update('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description'],'status'=>$form_state['values']['status']))->condition('id',arg(2));

  $query->execute();
   // print_r($query); 
  drupal_set_message(t('Country %name has been updated.', array('%name' => $form_state['values']['country_name'])));

  //print_r($description);
  }else{  

在插入此处的else代码中我们需要检查天气是否存在

0 个答案:

没有答案
相关问题