1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

How to store current dat in new row

Discussion in 'Community' started by bontemuis, Apr 7, 2020.

Thread Status:
Not open for further replies.
  1. bontemuis

    bontemuis Member

    Level: Community
    Hi there,

    I've visited already many corners of this forum, but I really can't figure this out. Please look at my php script in a form (onAfterProcess):

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $right_now = date();
    $user_key_1 = (int) $formModel->formData['user_id_raw'];
    $user_key_2 = (int) '{registration___id_raw}';
    $query->insert('table2')->set('reg_id = ' . (int) ($user_key_2))->set('reg_user = ' . (int) ($user_key_1))->set('date = ' . ($right_now));
    $db->setQuery($query);
    $db->execute();

    It doesn't accept date(), because I get this error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4.

    What goes wrong here?
     
  2. startpoint

    startpoint Active Member

    Level: Community
    PHP:
    $right_now = date("Y-m-d h:i:s");
     
    troester likes this.
  3. bontemuis

    bontemuis Member

    Level: Community
    I tried this, but I get this blue colored "date" in the code, indicating something is wrong...
     
  4. troester

    troester Well-Known Member Staff Member

    Level: Community
    Did you try to run the code?
    "blue" doesn't indicate an error but a function (red a string, grey a comment...)
     
  5. bontemuis

    bontemuis Member

    Level: Community
    Yes, I did. But now I'm thinking this might have something to do, either with the format settings of the date element or maybe server settings. You have to know I'm based in the Netherlands and our common date format is d-m-Y. So now i've changed that back to Y-m-d and added an "s" for seconds in time format. However, I still get this error message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '11:36:43' at line 4", so now I'm thinking: is this about a server setting UTC versus Local time?
    Anyhow, should the date function parameters correspond with the format settings of the date element?
     
    marozen likes this.
  6. troester

    troester Well-Known Member Staff Member

    Level: Community
    MySQL needs MySQL format, not a display format
    but this syntax is wrong
    set('date = ' . ($right_now));

    You must use quotes (it needs a date string) not paranthesis.

    Not relevant for your error but I think the format must be
    $right_now = date("Y-m-d H:i:s"); (H = 24 hours, h = 12 hours)
     
  7. bontemuis

    bontemuis Member

    Level: Community
    Ok. First I tried '$right_now', than "$right_now", but no success. Maybe I misunderstood.
    But then I tried set('date = ' . $db->quote($right_now)); and this worked! Is this the best way to do it?
     
Thread Status:
Not open for further replies.

Share This Page