Database Insert Condition not behaving

Wappler: v4.1.3
OS: MacOS
Server: PHP
Database: MySQL

I have a Database Insert which has a condition on a couple of the fields but it’s causing the field to not insert despite the condition being met:

Condition is:
{{$_POST.response[$index] == 'answered'}}

FYI, the variable $_POST.response[$index] contains the correct values as they are inserted in the same insert.

Some records have ‘answered’ as the value, others have something else, but they all behave as if the condition isn’t met, eg. null being inserted.

Before I spend more time trying to find another way, could you confirm if this is a bug?

Any thoughts on this? Just need to know if I should be spending more time trying to get it working or if it’s a bug and will never work.

Is this client-side condition setting disabled or server-side query condition?

Good question. It’s server-side in a query condition (insert record).

OK. so is there a repeat involved? i.e. where is the $index coming from?

Yes, it’s in a repeat loop. An array is posted to the action script. The variable definitely has a value because it’s inserted in another field:

I want the answerScore and answerComment fields to be left null if the $_POST.response[$index] field != 'answered'. If is does == 'answered' then store those values. Hope I’ve explained that correctly. The screenshot illustrates how I think it should work… but it doesn’t!

so the answerAnswer column is receiving and inserting a value?

Yes. And with those conditions in place the answerScore and answerComment fields are staying null even if the answerAnswer field successfully stores ‘answered’. This is why I’m wondering if it’s a bug. I’m sure my setup is correct for what I want to achieve.

It looks ok. One thing you could try is a setValue step before the query (inside the repeat) and set it to the $_POST.response[$index] value. You could then use that (as well as output it to check what value it is getting) as the condition/insert value in the query

1 Like

Good idea. Will try that now. Thanks Ben.

1 Like

You could even set it to $_POST.response[$index] == 'answered' ? 1 : 0 and use it as a boolean

1 Like

I went with the boolean as that’s a much tidier solution. It partly works but is giving an odd scenario.

Here’s the data:

Ignore the first four columns but the remaining two behave fine for the first four records and then just stick to null after that. Rows 3 and 4 should be null but 5 onwards should go back to storing values.

My SC structure is this:


As an aside, I set the setValue to be a ‘boolean’ but it reverts back to ‘text’. But that’s for another thread.

The API response confirms the variable is set correctly:

{"identity":1,"repeatResponse":[{"answered":1},{"answered":1},{"answered":0},{"answered":0},{"answered":1},{"answered":1},{"answered":1},{"answered":1},{"answered":1}]}

Can you show the new insert query?

And I’ve also tried it with the condition {{answered == 1}} but both produce the same result.

Can you use an outputted setValue step for the scale value and comments values? Just to check they do have the correct values

{"identity":1,"repeatResponse":[{"answered":1,"scale":"7","comments":"Comment for q1"},{"answered":1,"scale":"3","comments":"Comment for q2"},{"answered":0,"scale":"5","comments":""},{"answered":1,"scale":"4","comments":"Comment for q4"},{"answered":0,"scale":"5","comments":""},{"answered":1,"scale":"5","comments":"Comment for q6"},{"answered":1,"scale":"5","comments":""},{"answered":1,"scale":"5","comments":""},{"answered":1,"scale":"5","comments":""}]}

All as I would expect. The last three I just left the comments box empty.

Possibly the final thing I can think of to try is answered == ‘1’ (using single quotes to look for it as a string)

One other thing you could try is a ternary in the insert for the value
e.g.

{{answered ? $_POST_scalevalue[$index] : null}}

It’s odd though because I use conditions all the time and they seem to work ok. Which Wappler version are you on?

I’ve tried pretty much every combination. I’m on v4.2.0 and have been on the latest versions as soon as they’re released.

The ternary works! Would prefer to use a condition but as a ternary it’s working perfectly. Huge thanks for all your time @bpj.

I can only assume it’s a bug somewhere so will leave this for the Wappler team.

1 Like