0

I am developing one custom module in drupal 7 like a registration form all things are worked correctly but i cannot insert the records in my db_table. I am using the following code in drupal 7

function reg_fields_form_submit($form,&$form_state){
    $name = trim($form_state['values']['name']);
    $phone = trim($form_state['values']['phone']);
    $email = trim($form_state['values']['email']);
    $query = "INSERT INTO {registration} (name,phone,email) VALUES ('%s', '%s', '%s')";
    $result = db_query($query, array($name, $phone, $email));
if ($result !== FALSE){
    drupal_set_message('data saved successfully.');}

but in my table inserted values are like %S,%S,%S in everytime how can fix this issue.and also i want to display the inserted values in to same page. Any one help me

Ben10
  • 3,221
  • 2
  • 34
  • 61

2 Answers2

1

Drupal 7 uses a new database API, which includes the functiondb_insert() that is strongly recommended to be used in place of db_query("INSERT")

In your case, your code would become

function reg_fields_form_submit($form,&$form_state){
    $name = trim($form_state['values']['name']);
    $phone = trim($form_state['values']['phone']);
    $email = trim($form_state['values']['email']);
    $result = db_insert('registration')
    ->fields(array(
        name => $name,
        phone => $phone,
        email => $email,
    ))
    ->execute();
    if ($result){
        drupal_set_message('data saved successfully.');}
-1

Looks like drupal 7 uses ? for unnamed placeholder. So use ? instead of %s or use named placeholder.db_query

Using db_insert for insert operation is recommended.

bhab
  • 169
  • 2
  • 11
  • I fixed the issue in inserting values now i want to display the inserted values after submit. I am using page call back calling some function to use drupal_get_form but i can not do it.you know give some suggestion – Ben10 Mar 22 '13 at 04:50
  • If you are trying to display the input values in the form itself you should check [this](http://stackoverflow.com/questions/1077806/drupal-formwant-to-show-previous-form-value-as-default-value-on-page) – bhab Mar 22 '13 at 05:29