Having trouble with a conditon - where / regexp

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-27 14:36

Hi,
I'm struggling with a selection in a Hook.
It should be simple of course, but I'm blind for the solution right now.
The Case:
I have a text field and in the table hook:

Code: Select all

function table_before_insert(&$data, $memberInfo, &$args){
I have to make a decision when the field should be updated or not.

If I use:

Code: Select all

$baseAddressfotos = 'http://server/DataBase/Dir/';
if($data['Field'] = "http" ) return FALSE;
 $sql_string = "UPDATE `table` set `field` ='" .$baseAddressfotos . $data['field1']."' WHERE id = ".$data['selectedID'].";"; 
 $result = sql($sql_string,$o);
When I save the record (using Save as a Copy) I get a message,
Couldn't save the new record
but the "error" is not explained.
I assume it is because I use de = operator. However the REGEXP operator seems not supported within Appgini.

Could someone please give me a hint, or better: a solution?

I'm using Appgini 5.51 Revision 905
Ron

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Having trouble with a conditon - where / regexp

Post by grimblefritz » 2016-11-27 15:15

Have you tried using simple strings (such as "abc") to verify the sql works, without other considerations in play? Something like:

Code: Select all

$sql_string = "UPDATE `table` set `field` = 'abc' WHERE id = ".$data['selectedID'].";";

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Having trouble with a conditon - where / regexp

Post by Bertv » 2016-11-27 15:28

Ron,
try: if($data['Field'] == "http" ) return FALSE;
Sometimes this was a solution for my problems.
Bert
I am using Appgini 5.75

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-27 16:07

Hi grimblefritz and Bertv,
Thanks for replying and the suggestions.
@grimblefritz That works fine.
@Bertv That does'nt matter, still the nasty warning
Could'nt save the new record

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Having trouble with a conditon - where / regexp

Post by Bertv » 2016-11-27 18:30

Ron,
do you have the code in '_before_insert' hook?
At that moment there no record in database with the new selectedID.
Try the '_after_insert' hook.
Bert
I am using Appgini 5.75

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Having trouble with a conditon - where / regexp

Post by grimblefritz » 2016-11-27 23:18

If the simplified statement is working, then the problem is in the value string being assigned in SET `field`.

Try running it twice more. Once setting only the value of $data['field1'], and again with only the value of $baseAddressfotos.

I suspect the problem may be with the slashes in the latter string. Have you tried escaping with mysqli_real_escape_string?

peebee
AppGini Super Hero
AppGini Super Hero
Posts: 352
Joined: 2013-03-21 04:37

Re: Having trouble with a conditon - where / regexp

Post by peebee » 2016-11-28 03:20

I think Bertv might be on the money with his comment about the hook being in '_before_insert' as being the cause of the error.

The hook will hit this FALSE:

if($data['Field'] = "http" ) return FALSE;

and you will end up with a "Couldn't save the new record" error before any insert can be made.

Not entirely sure what the solution is but I think that might be the problem, for what it's worth.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-28 15:06

Hi ,
I've placed the code in the _after_update section.
The result still is no reaction on if the condition == 'http'

Code: Select all

function fotos_after_insert($data, $memberInfo, &$args){
$baseAddressfotos = 'http://server/DataBase/Dir/';
if($data['field1'] == NULL) return FALSE;
  if($data['field1'] == "http") return FALSE;
	$sql_string = "UPDATE `fotos` set `field1` ='" .$baseAddressfotos . $data['field1']."' WHERE fotoid = ".$data['selectedID'].";"; 
	$result = sql($sql_string,$o);
		return TRUE;
	}
	
The field1 can have the next values
1 NULL
2 tekst
3 http
In case of NULL no action is required
In case of tekst then I add first .$baseAddressfotos . before tekst, this works fine.
However
In case of http the $sql.string statement will be executed again and so I've got http twice in field1

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Having trouble with a conditon - where / regexp

Post by Bertv » 2016-11-28 16:23

So field1 can be
- null
- "http''
- ''some text bla bla"
or
- "http://server/Database/Dir/some text bla bla"

for preventing doing the update in the last situation, change
if($data['field1'] == "http") return FALSE;
into
if(substr($data['field1'] ,0,4) == "http") return FALSE;
Bert
I am using Appgini 5.75

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-29 10:41

Hi Bertv,
Thanks that did the trick for field1.
I have to do 8 fields in total that way, so I thought.... cut and paste the code and adjust the filed names and bingo.
However.....
Field 2 wasn't updates as expected.
I think this is because of the "IF's", do I have to put a { somewhere?

Code: Select all

if($data['field1'] == NULL) return FALSE;
  if(substr($data['field1],0,4) == "http") return FALSE;
	$sql_string = "UPDATE `fotos` set `field1` ='" .$baseAddressfotos . $data['field1']."' WHERE fotoid = ".$data['selectedID'].";"; 
	$result = sql($sql_string,$o);
	
  if($data['field2'] == NULL) return FALSE;
  if(substr($data['field2'],0,4) == "http") return FALSE;
	$sql_string = "UPDATE `fotos` set `field2` ='" .$baseAddressfotos . $data['field2']."' WHERE fotoid = ".$data['selectedID'].";"; 
	$result = sql($sql_string,$o);
		return TRUE;
Ron

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Having trouble with a conditon - where / regexp

Post by Bertv » 2016-11-29 12:31

Ron,
in this code the second update is only executed if the first one is executed.
If field1 is null or filled with 'http; blablabla' then nothing happend now.

Maybe better is the next code

if ($data['field1'] != NULL)
and (substr($data['field1],0,4) != "http")
then
$sql_string = "UPDATE `fotos` set `field1` ='" .$baseAddressfotos . $data['field1']."' WHERE fotoid = ".$data['selectedID'].";";
$result = sql($sql_string,$o);
end if;
   
if ($data['field2'] != NULL)
and (substr($data['field2'],0,4) != "http")
then
$sql_string = "UPDATE `fotos` set `field2` ='" .$baseAddressfotos . $data['field2']."' WHERE fotoid = ".$data['selectedID'].";";
$result = sql($sql_string,$o);
end if;

return TRUE;

Now the second update is always executed (if field2 is not null and not 'http').
Bert
I am using Appgini 5.75

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-29 13:06

Hi BertV
I think there is a typo or so, this code doesn't display a table overview, instead of that a HTTP error 500
I've also tried to spell END IF as ENDIF (this because the NOTEPAD++ editor didn't recognize end if (It wasn't colored).
The same happens with the THEN statement (It wasn't colored).

Code: Select all

if ($data['field1'] != NULL) 
and (substr($data['field1],0,4) != "http") 
then
$sql_string = "UPDATE `fotos` set `field1` ='" .$baseAddressfotos . $data['field1']."' WHERE fotoid = ".$data['selectedID'].";"; 
$result = sql($sql_string,$o);
end if;
   
if ($data['field2'] != NULL) 
and (substr($data['field2'],0,4) != "http") 
then
$sql_string = "UPDATE `fotos` set `field2` ='" .$baseAddressfotos . $data['field2']."' WHERE fotoid = ".$data['selectedID'].";"; 
$result = sql($sql_string,$o);
end if;

return TRUE;

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Having trouble with a conditon - where / regexp

Post by Bertv » 2016-11-29 15:35

Ron,
sorry for this; I am busy with php, js, sql, visualbasic and sometimes I make a mix of the different languages.
Below the correct code:

if ($data['field1'] != NULL
and substr($data['field1'],0,4) != "http") {
$sql_string = "UPDATE fotos set field1 ='" .$baseAddressfotos . $data['field1']."' WHERE fotoid = ".$data['selectedID'].";";
$_SESSION['dbgStr'] = "sql-string=: ".$sql_string;
$result = sql($sql_string,$o);
}
if ($data['field2'] != NULL
and substr($data['field2'],0,4) != "http") {
$sql_string = "UPDATE fotos set field2 ='" .$baseAddressfotos . $data['field2']."' WHERE fotoid = ".$data['selectedID'].";";
$result = sql($sql_string,$o);
}

return TRUE;
Bert
I am using Appgini 5.75

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-29 16:21

Bertv,
That worked well, Thanks a lot.
I'm almost there, I hope :)

This was a case when I save a record as a Copy.
In case of an Updated record I should do the same AFTER save-ing.
So I've put the same code after

Code: Select all

function fotos_after_update($data, $memberInfo, &$args){
and filled one field with "text bla bla" nothing was changed.

Does your code not apply in this function, wat should be changed?
(As you've noticed I'm not an expert)
Ron

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Having trouble with a conditon - where / regexp

Post by Bertv » 2016-11-30 08:58

Ron,
the same code work in the after_update hook.

The code
$_SESSION['dbgStr'] = "sql-string=: ".$sql_string;
is just for debugging. You can remove it. If you want to use is put some code in the tablename_footer hook, like this
case 'detailview':
$footer='';
[i]// only for debugging
$footer .= $_SESSION['dbgStr'];
$_SESSION['dbgStr'] = '';
[/i] break;
When your code is passed you see the message at the end of your form.
Bert
I am using Appgini 5.75

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: Having trouble with a conditon - where / regexp

Post by RonP » 2016-11-30 09:26

Hi Bertv,
Thanks for this suggestion.
It is strange, because right after I posted the former post I came back with "HOLD YOUR HORSES :) "
The thing was:
I didn't place the code:

Code: Select all

$baseAddressfotos = 'http://server/DataBase/Dir/';{
After that all went well.

So thanks for spending time with me.
Ron

Post Reply