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));
$result = $db->execute();

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



$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));.

|improve this answer|||||

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.