0

I created a custom table named "test_customerid" for the use of select queries from the drupal database. The fields are naid (for national id), name and surname.

My aim is to display an error message if the user attempts to insert an National ID which is already present in the table. Otherwise, all information are added to the table "test_customerid". This must occur when a webform is submitted.

Here are my codes that I included in the webform.module of my drupal project (inside function webform_webform_submission_insert($node, $submission)):

$accessnode=$node->nid;
if($accessnode==6)
{
    $dataSub=$submission->data[1]['value'];//submitted value of National ID

    $result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=>$dataSub));
    if(!(empty($result)))//I also tried "if($result)"
    {
        drupal_set_message(t('The NID already exists.'), 'error'); 
    }
    else
    {

        db_insert('test_customerid')
        ->fields(array(

        'naid' => $submission->data[1]['value'], 
        'name' => $submission->data[2]['value'],
        'surname' => $submission->data[3]['value'],
        ))
        ->execute();
    }

The problem is: It displays the error message even if the user inputs a new National ID. I can't add new information in the table.

I even tried to convert the data into strings by using these before placing the variable into the insert query but it gives me the same error message:

  • $dataconvert=implode(",",$dataSub);
  • $dataconvert=serialize($dataSub);

I want that the query to take the National ID value from the webform and check out if it exists in the table. Can you someone help me with this please?

1

Just replace

$result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=>$dataSub));

with

$result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=>$dataSub))->fetchAll(); 

and try, hope it will work

0

It should be like

$accessnode=$node->nid;
if($accessnode==6) {
  $dataSub=$submission->data[1][0];//submitted value of National ID

  $result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=>$dataSub));
  if(!(empty($result))) {
    drupal_set_message(t('The NID already exists.'), 'error'); 
  }
  else {
    db_insert('test_customerid')
    ->fields(array(

    'naid' => $submission->data[1][0], 
    'name' => $submission->data[2][0],
    'surname' => $submission->data[3][0],
    ))
    ->execute();
  }
}

$submission->data[1]['value'] does not exist in the $submission object. Change this to $submission->data[1][0] and it will start working.

Also you have to change

db_insert('test_customerid')
  ->fields(array(
    'naid' => $submission->data[1]['value'], 
    'name' => $submission->data[2]['value'],
    'surname' => $submission->data[3]['value'],
))
->execute();

query as well.It should be

db_insert('test_customerid')
  ->fields(array(
    'naid' => $submission->data[1][0], 
    'name' => $submission->data[2][0],
    'surname' => $submission->data[3][0],
))
->execute();

Hoping that this would solve the problem of yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.