Isengo
Isengo

Reputation: 2073

Increment Field value by UPDATE SET

I am trying to update a field by using this query:

$sql2 = "UPDATE ujc72_rseventspro_events SET discounts = discounts + 1 WHERE id = '$id' ";

But it won´t work. When I use:

$sql2 = "UPDATE ujc72_rseventspro_events SET discounts = 1 WHERE id = '$id' ";

It works like a charm, so there is no problem with the connection etc.

I am using PHP 7 and Joomla 3.5

Upvotes: 0

Views: 495

Answers (1)

Amit Ray
Amit Ray

Reputation: 3485

If your using joomla you can use this query

$db = JFactory::getDbo();

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

// Fields to update.
$fields = array(
    $db->quoteName('discounts') . ' = ' . $db->quoteName('discounts')+1
);

// Conditions for which records should be updated.
$conditions = array(
    $db->quoteName('id') . ' = $id'
);

$query->update($db->quoteName('#__rseventspro_events'))->set($fields)->where($conditions);

$db->setQuery($query);

$result = $db->execute();

Upvotes: 1

Related Questions