0

Have a field called url_time in the users table. I have this function in place, though it does not update the table on the else part.

Does this code look correct?

function my_module_form_submit($form,$form_state)
{
global $user;
$account = user_load($user->uid);
$op = $form_state['values']['op'];

if ($op == $form['linkedin']['reset']['#value']) {
    if (db_field_exists('users', 'url_time')) {
        db_update('users')
            ->condition('uid', $account->uid)
            ->expression('url_time', 0)
            ->execute();
    }
}
else {
    $date = time();
    if (db_field_exists('users', 'url_time')) {
        db_update('users')
            ->condition('uid', $account->uid)
            ->expression('url_time', $date)
            ->execute();
    }
}
}

This is the linkedin function that I am trying to implement, so when the user authorities, the "else" will write a date.

function linkedin_user_enable_form($form, $form_state, $uid) {
$form = array();
$form['#user'] = $uid;
$form['#action'] = url('linkedin/token/' . $uid);
$form['submit'] = array(
 '#type' => 'submit',
 '#value' => t('Go to LinkedIn'),
'#suffix' => '<p class="description">' . t('You will be taken to the LinkedIn website in order to complete the process.') . '</p>',
);
return $form;
}
  • You cannot delete just a database column; you can delete one or more database rows. – kiamlaluno Jan 28 '16 at 20:22
0

This wouldn't delete just the field, it would delete the row for a user whose uid equals $account->uid. The ->expression() and subsequent ->execute() as @kiamlaluno points out, wouldn't work right anyway.

If you want to simply delete the value, you are better off doing something like this:

// $account is defined up ^^ there somewhere.
$account->url_time = '';
user_save($account);
  • 1
    Actually, the second is not a condition, so it doesn't catch those rows where url_time is 0. Plus, the object returned from db_delete() doesn't have any expression() method. – kiamlaluno Jan 28 '16 at 20:26
0

Your code is wrong: To set the value of url_time for the user row matching a user ID, you need to use the UpdateQuery::fields() method.

Adds a set of field->value pairs to be updated.

The correct code is the following one.

if (db_field_exists('users', 'url_time')) {
  db_update('users')
    ->condition('uid', $account->uid)
    ->fields(array('url_time', 0))
    ->execute();
}

This is similar to the following code used by Drupal core, which updates expire for the records matching the conditions.

  // Insure that the timeout is at least 1 ms.
  $timeout = max($timeout, 0.001);
  $expire = microtime(TRUE) + $timeout;
  if (isset($locks[$name])) {
    // Try to extend the expiration of a lock we already acquired.
    $success = (bool) db_update('semaphore')
      ->fields(array('expire' => $expire))
      ->condition('name', $name)
      ->condition('value', _lock_id())
      ->execute();
    if (!$success) {
      // The lock was broken.
      unset($locks[$name]);
    }
    return $success;
  }
  • If this helps clear things up a little. I want to delete the data inside the field. The default value I set for the field is 0. So I want to use the if (db_field_exists but want the url_time to be reset to the default value in the long run. – GreenLion Jan 28 '16 at 20:44
  • So, do you just need to set url_time to 0 for the user for the given user ID? – kiamlaluno Jan 28 '16 at 21:04
  • I change my original post. – GreenLion Jan 28 '16 at 21:22
  • For the future, remind you cannot change a question once it has answers in a way to invalidate the answers. Before posting a question be sure you are describing exactly what you need to do. Deleting a database column is different from updating a database column. – kiamlaluno Jan 28 '16 at 23:20

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.