-1

I have function that has to write some data into database table, but after function complete, i dont see added info in my phpmyadmin. Here is code of what am i trying to run

function q_cart_add($node) {
    global $user;
    //drupal_set_title('Добавление товара в корзину');
    drupal_set_title('Товар добавлен в корзину');
    if (!user_access('add to cart'))
        return '<p>У вас не достаточно прав для добавления в корзину</p>';
    if ($node->type != 'kartochka')
        return '<p>В корзину можно положить только товар</p>';
    $added = false;
    if ($user->uid) {
        $added = db_result(db_query('SELECT `added` FROM {q_cart} WHERE `uid` = %d OR `nid` = %d', $user->uid, $node->nid));
        if (!$added) {
            db_query('INSERT INTO {q_cart} (`uid`, `nid`, `added`) VALUES (%d, %d, %d)', $user->uid, $node->nid, time());
        }
    } else {
        foreach (isset($_COOKIE['q_cart']) && ($cart = json_decode($_COOKIE['q_cart'])) ? $cart : array() as $item)
            if ($item->nid == $node->nid) {
                $added = true;
                break;
            }
        if (!$added) {
            $cart[] = array(
                'nid'   => $node->nid,
                'added' => time(),
            );
            setcookie('q_cart', json_encode($cart), strtotime('+1 week'), '/');
        }
    }
    //if (!$added) return '<p>Товар добавлен в корзину.</p>'.l('Корзина', 'cart', array('attributes' => array('target' => '_top', 'class' => 'button-link'))).'<a href="javascript://" onclick="parent.Lightbox.end(\'forceClose\');" class="button-link">Позже</a>';
    if (!$added) {
        drupal_add_js('$(document).ready(function(){parent.document.getElementById("bottomNavClose").style.display = "none";parent.document.getElementById("lightbox").style.top = "200px";});', 'inline');
        return l('Корзина', 'cart', array('attributes' => array('target' => '_top', 'class'  => 'button-link'))) .
                '<a href="/node/' . $node->nid . '" target ="_top" class="button-link">Позже</a>';
        //'<a href="javascript://" onclick="parent.Lightbox.end(\'forceClose\');" class="button-link">Позже</a>';
    } else {
        return '<p>Товар уже есть в вашей корзине.</p>';
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Avdept
  • 2,261
  • 2
  • 26
  • 48
  • What does `db_query()` do? Give us *that* code, and an output of the query you're passing to it. – Matt Aug 10 '12 at 13:50
  • @Matt [`db_query()`](http://api.drupal.org/api/drupal/includes%21database.pgsql.inc/function/db_query/6) is a Drupal API function, it's not really feasible to post the full code here as there are a lot of different functions involved – Clive Aug 10 '12 at 13:57
  • 1
    @Clive thanks. I'm not a Drupal developer, so I wasn't sure. In that case it's very likely that it's the query itself that's the issue. @avdept, just `echo` the query to the screen and `exit()`; – Matt Aug 10 '12 at 13:58
  • Is it possible you're trying to do this as an anonymous user? `if ($user->uid)` will fail for anonymous users so the query would never run – Clive Aug 10 '12 at 14:00
  • No, i'm logged as root(uid = 1); – Avdept Aug 10 '12 at 14:02

1 Answers1

0

Please change db_query to mysql_query and add or die(mysql_error()). It shoud explain why adding fails.

For example: mysql_query('INSERT INTO {q_cart} (uid, nid, added) VALUES ('.$user->uid.', '.$node->nid.', '.time().')') or die(mysql_error());

mkey
  • 212
  • 1
  • 5
  • It actually passed, no error happened, aswell as didnt add info to database table – Avdept Aug 10 '12 at 13:52
  • You should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 10 '12 at 13:52
  • To be honest, its not my code, its from some module that im using in my project. – Avdept Aug 10 '12 at 13:53
  • In this case using mysql_query or die is the fastest way to solve the problem. It seems that this is problem with mysql syntax. – mkey Aug 10 '12 at 13:57
  • 1
    @mkey the fastest way to solve it is ***NOT*** to use bad practices. Just `echo` the query – Matt Aug 10 '12 at 13:58
  • something like this? ``echo db_query('INSERT INTO {q_cart} (`uid`, `nid`, `added`) VALUES (%d, %d, %d)', $user->uid, $node->nid, time()) or die(mysql_error());`` – Avdept Aug 10 '12 at 14:04
  • To print this query just use printf('INSERT INTO {q_cart} (`uid`, `nid`, `added`) VALUES (%d, %d, %d)', $user->uid, $node->nid, time()); – mkey Aug 10 '12 at 14:08
  • it gave me: ``INSERT INTO {q_cart} (`uid`, `nid`, `added`) VALUES (1, 760, 1344607784)`` – Avdept Aug 10 '12 at 14:10
  • SQL looks OK. Try this way: `$insert = db_insert('q_cart')->fields(array('uid' => $user->uid,'nid' => $node->nid,'added' => time())); try { $insert_i = $insert->execute(); } catch (PDOException $pe) { echo $pe->getMessage(); }` – mkey Aug 10 '12 at 14:26
  • 1
    As you can read in the drupal documentation: Do not use this (db_query) function for INSERT, UPDATE, or DELETE queries. Those should be handled via db_insert(), db_update() and db_delete() respectively. – mkey Aug 10 '12 at 14:27