0

I am getting this error: Argument 2 passed to db_query() must be an array, string given. In db_query in below function.

function uc_guest_checkout_uc_checkout_complete($order, $account) {
  if (!empty($_SESSION['uc_guest_checkout'])  && !user_is_logged_in()) {
    switch ($order->data['complete_sale']) {
      case 'new_user':
        // For guest checkout, we replace the ubercart generated username and email
        // for a new user with one that is more anonymous.
        $name = uniqid('anon-', TRUE);
        $mail = $name . '.' . uc_store_email();        
        db_query('UPDATE {users} SET name = "%s", mail = "%s" WHERE uid = %d', $name, $mail, $account->uid);   
   break;
  case 'logged_in':
    // If the order is marked logged in but anonymous, this means that 
    // we must have set the uid to the default account, but still treat
    // this as a new user.
    $order->data['complete_sale'] = 'new_user';
    break;
    }
  }
  _uc_guest_checkout_end();
}

I tried this queries but none were creating account with valid names and emails as per db_query which is throwing error but successfully creating user accounts.where as these below queries creating names with random names.

db_update('users')
        ->fields(array(
            'name' => $name,
            'mail' => $mail
        ))
        ->condition('uid', $account->uid)       
        ->execute();

db_query('UPDATE {users} SET name = :name, mail = :mail WHERE uid = :uid',
                array(':name' => $name, ':mail' => $mail, ':uid' => $account->uid));

How do I resolve this?

0

1 Answer 1

2

You're using Drupal 6 syntax, you need to convert it to Drupal 7 syntax:

db_update('users')
  ->fields(array('name' => $name, 'mail' => $mail))
  ->condition('uid', $account->uid)
  ->execute();

See the Database API docs for more details.

10
  • I have already mentioned in my question that i have already tried like that and its not creating user account with valid names but with random names.So that only i am trying to exactly replace the query and get exact results, which i am not getting rite now . In above function, You can able to see where it is creating user account with names and email. Where as wit this query its creating random names instead of valid names
    – Ram
    Commented Jan 8, 2014 at 10:54
  • What do you mean by 'random' exactly? Whatever you have in $name will be put into the database, you're totally in control of that. Drupal, nor MySQL, will not change that to 'random', it will only do what you tell it to do. You can't use db_update() to create a user obviously, is that what you're trying to do?
    – Clive
    Commented Jan 8, 2014 at 11:01
  • This is ubercart guest checkout module and there is a bug in it. I am trying to avoid that error. The error comes when i checkout as guest account and buy the product and then submit,it will create an buyer account with given names in the ubercart form. With above function it does all,creates user account properly. only issue over here is that it says Argument 2 passed to db_query() must be an array, string given.
    – Ram
    Commented Jan 8, 2014 at 11:06
  • You can't possibly be getting that error if you're using the Drupal 7 code that you've put in your question. If you're still getting that error, it's because you're using db_query() incorrectly as in your first code example. You need to use the code I've provided in the answer. If that doesn't "work", then your problem is somewhere else. That query is 100% valid, I can promise you.
    – Clive
    Commented Jan 8, 2014 at 11:08
  • In order to avoid that error i am trying to change that above query db_query('UPDATE {users} SET name = "%s", mail = "%s" WHERE uid = %d', $name, $mail, $account->uid); but if change to db_update it creates user account with random names like anon-564556 etc, random mail id [email protected] but not with ubercart filled name and email id.
    – Ram
    Commented Jan 8, 2014 at 11:09

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