0

I am trying to update the database with the original column's value plus (+1) or minus (-1)

$column_name = 'mycolumnname';
$table_name - 'mytablename';

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$fields = array($db->quoteName($column_name) . ' = ' . $db->quote($column_name +1));
$query->update($db->quoteName($table_name))->set($fields);
$db->setQuery($query);
$result = $db->execute();

If i run this it will always set the column value to 1. What am i doing wrong?

1 Answer 1

0

Try

$db->quoteName($column_name) . ' = ' . $db->quoteName($column_name).'+ 1'

instead of

$db->quoteName($column_name) . ' = ' . $db->quote($column_name +1)

If you echo $query you will see the MySQL command it is going to execute.

UPDATE `mycolumnname` SET `mycolumnname` = `mycolumnname`+ 1 

Adding a number to a string casts it as a number, hence it returns 1 when you generate the query using $db->quote($column_name +1));.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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