1

I am trying to insert the id value using JTable, however it seems like I have missed something and made the mistake while doing so. What changes do I need to make so as to get this working? Thanks in advance

$db = $this->getDbo();
$query = $db->getQuery(true);

$data = array(
['articleId'] = $this->get('id'),
['title'], 
['sharetoken']
);

$values = array($db->quote('id'), $db->quote($title), $db->quote($token));

$query
  ->insert($db->quoteName('#__share_draft'))
  ->columns($db->quoteName($data))
  ->values(implode(',', $values));


$db->setQuery($query);
$table->save($data);

return $db->execute();
2

Your $data needs to be just column names, not with any values.

$data = array('articleId', 'title', 'sharetoken');

and $values dosen't have correct id right now, since your are quoting just pasic string, not a value of id.

$values = array($db->quote($this->get('id')), $db->quote($title), $db->quote($token));

So, complete code should look like this:

$db = $this->getDbo();
$query = $db->getQuery(true);

$data = array('articleId', 'title', 'sharetoken');
$values = array($db->quote($this->get('id')), $db->quote($title), $db->quote($token));

$query
    ->insert($db->quoteName('#__share_draft'))
    ->columns($db->quoteName($data))
    ->values(implode(',', $values));


$db->setQuery($query);
$table->save($data);

return $db->execute();

You can have column names and values in same array (if it makes more sense to you), but this requires some changes.

For example:

$data = array(
  'articleId'  => $db->quote($this->get('id')),
  'title'      => $db->quote($title),
  'sharetoken' => $db->quote($token)
);

$query
    ->insert($db->quoteName('#__share_draft'))
    ->columns($db->quoteName(array_keys($data)))
    ->values(implode(',', array_values($data)));

Check if id exists in table

$db = $this->getDbo();
$query = $db->getQuery(true);

$query
  ->select('*')
  ->from($db->quoteName('#__share_draft'));
  ->where($db->quoteName('articleId') . ' = '. $db->quote($this->get('id')));

$db->execute();
$num_rows = $db->getNumRows();

if ($num_rows > 0) {
  // article with this ID exists
} else {
  // article dosen't exist
}
  • Hi....What if I want to know if the id already exists in the table...what code should I write for that?... – Nikita Dhiman Jul 28 '16 at 16:40
  • @NikitaDhiman See my edit. – Rene Korss Jul 29 '16 at 7:47
  • thanks...one more question. I am trying to load value from only one field in the table...This code seems to have an error. $db = $this->getDbo(); $query = $db->getQuery(true); $query ->select('sharetoken') ->from($db->quoteName('#__share_draft')) ->where($db->quoteName('id') . ' = '. $db->quote($this->get('id'))); $db->execute(); $valueshow = $db->loadObject(); echo "not a new value, the existing value is:".$valueshow; – Nikita Dhiman Jul 29 '16 at 10:18
  • It shows that string cannot be loaded...is there any other method to load results? – Nikita Dhiman Jul 29 '16 at 10:21
  • Instead of $db->execute(); $valueshow = $db->loadObject(); you should have $db->setQuery($query); $valueshow = $db->loadObject(); and since you are loading object, you can get value with echo $valueshow->sharetoken; – Rene Korss Aug 2 '16 at 6:12

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.